Core Lesson 2 : Data Types in Java
So, In this chapter, we would be learning about the various data types available in java and how to declare them, and how to use them in our java program. It is mostly similar to the C and C++ programming languages. Java is a strongly typed language. Every variable has a type, every expression has a type and java always ensures before store data in the variable, do they match the type of variable we have declared.
How to Declare a variable
public class DataTypeDemo {
public static void main(String args[]) {
int singleLineVariable;
int anotherSingleLineVariable=10;
int multipleVariableOnSameLine1, multipleVariableOnSameLine2;
int multipleVariableOnSameLineWithInitValue1= 10, multipleVariableOnSameLineWithInitValue2= 20;
int dynamicallyInitializedVariable = anotherSingleLineVariable * 20;
}
}
Primitive Types
- long (range : –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
- int (range : –2,147,483,648 to 2,147,483,647)
- short (range :–32,768 to 32,767)
- byte (range : –128 to 127)
class DataTypeDemo {
public static void main(String args[]) {
byte var1 = 10; // Declaring byte variable with initial value of 10
short var2 = 20; // Declaring short variable with initial value of 20
int var3 = 40; // Declaring int variable with initial value of 40
long var4 = 80; // Declaring long variable with initial value of 80
}
}
class DifferentNumberSystems {
public static void main(String args[]) {
// octalNumber value can be declare by leading 0 before number
// blow is the declaration of 10 octal number which is 8
int octalNumber = 010;
System.out.println(octalNumber);
// hexaDecimal value can be declare by leading 0x before the number
// below is the declaration of 0x1A hexa number which is 26
int hexaDecimalNumber = 0x1A;
System.out.println(hexaDecimalNumber);
// binaryNumber value can be declare by leading 0b before the number
// below we have declare binary version of 172
int binaryNumber = 0b10101100;
System.out.println(binaryNumber);
}
}
- double (64 bit)
- float (32 bit)
Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant. You can also explicitly specify a double literal by appending a D or d. Doing so is, of course, redundant. The default double type consumes 64 bits of storage, while the smaller float type requires only 32 bits. Let's have a look at below example
public class DataTypeDemo {
public static void main(String args[]) {
// declaring float variable
// Notice f at the end of the number
float var1 = 10.5f;
// declare double variable
double var2 = 3.14;
}
}
- You must have seen in mathematical books when it comes to defining large number; we use a comma as a separator value
- This allows the reader to quickly grasp the value of the number by grouping them
- java allows us to specify separator while you declare the number type values
- Separator allowed here is underscores character (_)
// Internation format
// one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine
int veryLargeNumber = 123_456_789;
// Indian format
// twelve crore thirty four lakh fifty six thousand and seven hundred and eighty nine
int anotherVeryLargeNumber = 12_34_56_789;
// Value of pi with separator
double fractionalNumber = 3.141_592_653;
- In Java, the data type used to store characters is char
- char in Java is not the same as char in C or C++
- In C/C++, char is 8 bits wide uses ASCII charset. Instead, Java uses Unicode charset.
- Unicode defines a fully international character set that can represent all of the characters found in all human languages.
- Unicode required 16 bits. Thus, in Java char is a 16-bit type.
public class DataTypeDemo {
public static void main(String args[]) {
char firstCharVariable = 'A';
char nextCharVariable = 'B';
char smallCaseLetterVariable = 'h';
}
}
- Java has a primitive type, called boolean, for logical values
- It can have only one of two possible values, true or false
- This is the type returned by all relational operators, as in the case of a < b
- Boolean is mostly used in control execution commands we would be going to learn ahead
- The values of true and false do not convert into any numerical representation
- The true literal in Java does not equal 1, nor does the false literal equal 0
public class DataTypeDemo {
public static void main(String args[]) {
boolean hasVisa = false;
boolean hasJob = true;
boolean isEligibleForVoting = true;
if(isEligibleForVoting) {
System.out.println("Person is eligible for Voting");
}
else {
System.out.println("Person is Not eligible for Voting");
}
}
}
String Data type
public class DataTypeDemo {
public static void main(String args[]) {
String str1 = "Hello this is java program";
String str2 = "This is another line";
String str3 = str1.toUpperCase();
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
Important Points to Remember from these lessons
- We can declare more than 1 variable on the same line of the same data type
- we can initialize more than 1 variable on the same line of the same data type
- Java is a strictly typed language. so you cannot assign float to int or float to char
- We can also initialize number in octal, binary, or hexadecimal format
- String is not primitive data type. It is a Complex data type in java
- The primitive variable doesn't have any member function on them
- Since String is not primitive it has a member function defined inside it
Till then Good Bye, Happy Coding !!!