Data Types in Java
Data Types in Java
A primitive data type specifies the size and type of variable values, and it
has no additional methods.
There are eight primitive data types in Java:
1) Variables naming cannot contain white spaces, for example: int number = 100;
is invalid because the variable name has space in it.
2) Variable name can begin with special characters such as $ and _
3) As per the java coding standards the variable name should begin with a lower
case letter, for example int number; For lengthy variables names that has more
than one words do it like this: int smallNumber; int bigNumber; (start the second
word with capital letter).
4) Variable names are case sensitive in Java.
Java Type Casting
Type casting is when you assign a value of one primitive data type to another
type.
Widening Casting
int myInt = 9;
System.out.println(myInt); // Outputs 9
Narrowing Casting
System.out.println(myInt); // Outputs 9
Java Strings
String Length
A String in Java is actually an object, which contain methods that can perform
certain operations on strings. For example, the length of a string can be found
with the length() method:
System.out.println(txt.indexOf("locate")); // Outputs 7
Array
Syntax
dataType[] arrayRefVar; // preferred way.
or
dataType arrayRefVar[]; // works but not preferred way.
System.out.println(cars[0]);
// Outputs Volvo
System.out.println(cars.length);
System.out.println(cars[i]);
To create a two-dimensional array, add each array within its own set of curly
braces:
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
System.out.println(myNumbers[i][j]);
}
}
}
Types of Variables in Java
There are three types of variables in Java.
1) Local variable 2) Static (or class) variable 3) Instance variable
System.out.println(myClassVar);
Do note that only static variables can be accessed like this. This doesn’t apply for
instance and local variables.
Instance variable
Each instance(objects) of class has its own copy of instance variable. Unlike
static variable, instance variables have their own separate copy of instance
variable. We have changed the instance variable value using object obj2 in the
following program and when we displayed the variable using all three objects,
only the obj2 value got changed, others remain unchanged. This shows that they
have their own copy of instance variable.
System.out.println(obj.myInstanceVar);
System.out.println(obj2.myInstanceVar);
System.out.println(obj3.myInstanceVar);
System.out.println(obj.myInstanceVar);
System.out.println(obj2.myInstanceVar);
System.out.println(obj3.myInstanceVar);
}
}
Output:
instance variable
instance variable
instance variable
instance variable
Changed Text
instance variable
Local Variable
These variables are declared inside method of the class. Their scope is limited to
the method which means that You can’t change their values and access them
outside of the method.
In this example, I have declared the instance variable with the same name as
local variable, this is to demonstrate the scope of local variables.
Example of Local variable
public class VariableExample {
// instance variable
public String myVar="instance variable";
Calling Method
Inside Method
instance variable
If I hadn’t declared the instance variable and only declared the local variable
inside method then the statement System.out.println(obj.myVar); would have
thrown compilation error. As you cannot change and access local variables
outside the method.