Basic Java constructs
Java is a strongly typed language, which means that all variables must first be declared before they can be used.
The basic form of variable declaration is “Variable Type followed by variable name”. For example “int myVar;” Doing so tells your program that the variable named “myVar” exists and holds numerical data[ i.e integer value].
A variable’s data type determines the values it may contain, plus the operations that may be performed on it.
The data types are of two types. They are primitive data types and reference data types.
Primitive Data Types are pre defined by the language and is named by a reserved keyword. Example: int, float etc.
Reference Data types are often referred as non-primitive data types. Example: Objects and Arrays They are called as reference data types because they are handled “by reference” – in other words, the address of the object or array is stored in a variable of reference data type and not the value directly. In contrast primitive types are handled “by value”-the actual primitive values are stored in variables of primitive data types . Reference types are any instantiable class as well as arrays: String , Scanner , Random , Die , int , String , etc.
Type Casting is the process of assigning a value of one type to the variable of another type. While assigning the value of var1 to var2, if both var1 and var2 are of different data types and if the data types of var1 and var2 are compatible and var2 data type is larger than var1 data type then java will perform the value conversion automatically else it has to be done manually.
Totally there are 8 primitive data types( int, byte, short, long, float, double, boolean and char).
Some important points to be noted are,
1.All numeric data types are signed
2.The size of data types remain the same on all platforms
3.char data type in Java is 2 bytes because it uses UNICODE character set which covers all known scripts and language in the world.
Objects and Arrays are accessed using reference variables in Java. A reference variable is similar to a pointer in C Java does not allow pointer manipulation or pointer arithmetic. A reference type can be assigned ‘null’ to show that it is not referring to any object.
In any Java Program, the variable is the elementary unit of storing data. A variable can be declared by using an identifier i.e. the variable name and a datatype.You can also initialize a variable when it is declared. In Java, before you use any variable, it should be declared. At the same time, In Java, if a local variable is used without initializing it, the compiler will show an error. Other variables such as member variables etc are assigned with the default values automatically.
According to Java coding standards, the class name should always start with an upper case character. In case there is more than a word, each word should start with an uppercase letter and there should not be any _ in between the words.The name of a variable should be in lower case. In case there is more than a word, the first letter of each word should be in upper case, starting from the second word The best practice to be followed in declaring the variable is, Declare a variable in program only when required. Do not declare variables upfront like in C.
Type Conversion and Casting
The process of assigning a value of variable of one type to a variable of another type is called casting.
There are two types of type casting.
1.Automatic, non-explicit type changing: It is known as Implicit Conversion. While assigning the value of var1 to var2, if both var1 and var2 are of different data types and if the data types of var1 and var2 are compatible and var2 data type is larger than var1 data type then java will perform the value conversion automatically. The idea is Variable of smaller capacity can be assigned to another variable of bigger capacity.For example when the integer value of 10 is assigned to a double variable d, then java will automatically convert 10 to 10.0 and store it in d.
2.Explicit type changing: If we want to assign the value of var1 to var2 and if the data types of var1 and var2 are incompatible then java will not carry the type conversion automatically. The programmer has to explicitly do the conversion by specifying a type cast operator. The general form is (target-type) followed by value. Where the “target-type” specifies the desired type to convert the value to.
Type conversion that happens when Variable of smaller capacity assigned to variable of bigger capacity forms widening.
Type conversion that happens when Variable of larger capacity assigned to variable of smaller capacity forms Narrowing. Narrowing is usually done through explicit cast.