Java from the Scratch – Part II – Rules, Standards, Data Types

Share

Identifiers

In Java, names of variables, methods, classes, packages and interfaces are called identifiers. When composing an identifier, there are some rules.

  • Identifiers must be a composition of letters, numbers, underscore and $ sign.

That means you couldn’t use any other character other than mentioned here. So my#name is invalid identifier because # is not a valid character.

  • Identifiers must begin with a letter, an underscore or $ sign.

So you cannot use names such as 1stName as a identifier. You could only use characters mentioned here as the first letter of an identifier.

  • No space between words.

You can’t use spaces in identifier names. Therefore my name is not a valid identifier name.

  • You cannot use keywords as identifiers.

In java there are keywords. You can’t use them as identifiers. So public is an invalid identifier because public is a keyword in java.

You should know these rules, otherwise your program will give you an error when compiling.

Examples for legal identifiers: MyVariable, myvariable, _name, $name, name1

Examples for illegal identifiers: public, 1name, my program, my*name

Oracle Code Conventions (Java Standards)

In Java, there are some programming ethics. These ethics are not rules. But if you use them, it is easier for another developer to understand the code again.

Class Name Standard

  • Class name should be a noun.
  • Every word should start with uppercase.

Eg: Student, Employee, Manager

Interface Standard

  • Interface should be adjectives.
  • Every word should start with uppercase.

Eg: Runnable, Serializable

Method Name Standard

  • Method names should be verbs.
  • Method names should start with lowercase and from the second word, it should start with uppercase.

Eg: addName, findStudent

Variable Name Standard

  • Variable names should be nouns.
  • Variable names should start with lowercase and from the second word, it should start with uppercase.

Eg: studentName, employeeId

Constant Name Standard

  • Every letter should be uppercase.
  • Words should be separated with underscore.

Eg: MAX_WEIGHT

Java Primitive Data Types

In java there are several primitive data types that we could use. Those are mentioned below.

  • boolean – 1bit – boolean is a 1bit data type which could carry either true or false.
  • byte       – 8bit – integer numbers
  • short     – 16bit – integer numbers
  • int         – 32bit – integer numbers
  • long      – 64bit – integer numbers
  • float     – 32bit – floating point numbers
  • double – 64bit – floating point numbers
  • char     – 16bit – characters

Note:

In java, String is not a primitive data type. But you could use it as a data type which could carry string value.

Default value for String data type in java is null.


byte, short, int, long

  • byte could carry integers from -128 to 127
  • short could carry integers from -32768 to 32767
  • int could carry integers from -232-1 to 232-1 – 1
  • long could carry integers from -264-1 to 264-1 – 1

Note:

In java, a variable could be initialized in following format.

DataType variableName = value;


All these data types could carry octal, decimal and hexadecimal values. Initializing variables using these data types could be done as follows.

byte x = 20;      (This is a decimal value)

short y = 056;   (This is an octal value. If you need to add an octal value, you should begin the number with 0 and use only numbers from 0 to 7)

int value = 0x45FA;     (This is a hexadecimal value. If you need to add a hexadecimal value, you should begin the number with 0x and use numbers from 0 to F)

long val = 4569;

long val = 34L;      (You could add lowercase or uppercase L at the end of the number in long data type. But adding L is optional for long data type)


Note:

Default data type for integer numbers in java is int. So in general, if we need to initialize an integer value, we use int data type. And also after any arithmetic operation with integers, results will be automatically converted into int.

Note: 

Default value for integer data types (byte, short, int, long) is 0.


Now let’s check out those data types in a java program. Create PrimitiveDataTypes class as follows. Here keywords are highlighted.

 

Figure 01
Figure 01

 

Now save this as PrimitiveDataTypes.java.  Compile it and run it. So you will get an output as shown below.

Figure 02
Figure 02

float and double

There are two data types in java which could carry floating point values as follows.

  • float could carry floating point values from 1.401e-45 to 3.402e+38 (positive or negative)
  • double could carry floating point values from 4.940e-324d to 1.797e+308d (positive or negative)

You could initialize those data types as follows.

float f1 = 10.34f;       (Number is ended with lowercase or uppercase F letter. That is compulsory for float data type if your number is a floating point number.)

float f2 = 45.569F;

float f3 = 10.54;     (This is wrong. You must suffixed the number with F or f if you’re using float data type and floating point number.)

float f4 = 10;          (This is OK. Because the number in here is not a floating point number.)

double d1 = 19.34;    (In double, adding D,d,F or f as a suffix is optional.)

double d2 = 34;

double d3 = 45.3f;

double d4 = 46.22D;


Note:

Default data type for floating point numbers in java is double. So in general, we use double as the floating point data type. And also after any arithmetic operation with floating points, results will be automatically converted into double.

Note:

Default value for floating point numbers (float, double) is 0.0 in java.


char

char is used to store one character value and is based on 16bit unicode-encoding. Char could hold integer values from 0 to 65535. You could initialize char variables as follows.

char c1 = 50;            (This is an ASCII value.)

char c2 = ‘A’;            (You must enter character inside single quotations.)

char c3 = ‘\u0000’;   (Unicode value.)

char c4 = ‘5’             (Character again)


Note:

Default value of char data type is \u0000.

Default value of boolean data type is false


Try yourself:

Make a program to test data types that you have just learnt.

Your Second Java Program

Here I will create a simple program to add two numbers with java. First of all you need to create java class. Name it as AddNumbers.

class AddNumbers{

}

Now save it as AddNumbers.java.

Second important thing is main method. So now create the main method inside the class.

class AddNumbers{

public static void main(String args[]){

}

}

Then we need two numbers to add. So let’s take 10 and 12. Both are integer numbers. So I’ll pick my data type as int. Now I’m going to create two variables with those numbers named num1 and num2.

class AddNumbers{

public static void main(String args[]){

int num1 = 10;
int num2 = 12;

}

}

Now here I’m going to add 10 and 12. So the results is also will be integer value. So here I add those numbers and save the result into new integer variable called result.

class AddNumbers{

public static void main(String args[]){

int num1 = 10;
int num2 = 12;

int result = num1 + num2;

}

}

The operation is completed! Now let’s print the result so we could see weather it is correct or not.

class AddNumbers{

public static void main(String args[]){

int num1 = 10;
int num2 = 12;

int result = num1 + num2;

System.out.println(“Result: ” + result);

}

}

Now save the document, compile it and run it.

Figure 03
Figure 03

 

Try Yourself:

  • Create a program to calculate the value of the expression v = u + at when u, a and t is given.
  • Create a program to calcualte  A = πd2/4 when π and d is given.

If you have any ideas to improve this article, please comment below.

 
Tagged :