Different Java Variables and Data Types II

Java Data Types

Okay, so in a previous post we have talked about the Java Variables and in this, we are going to talk about different Java data types. So, as we have already mentioned that Java is well statistically designed language. This means that all the variables are mandatory to be declared before they can be used in equations. For example;

int speed;

In this example, speed is a variable of data type int. Here int will determine the speed variable that can only contain integers. In clear terms, a variable data type determines the values of the variable that can be stored in it. There are mainly 8 Java data types which are known as primitive data types.

8 Primitive Java Data Types

Boolen

This Java data type has two possible values only; true or false. False is a default value in the boolean. They are mostly used when true or false statements are declared. For example;

class BooleanExample {
public static void main(String[] args) {

boolean flag = true;
System.out.println(flag);
}
}

Output

True

Byte

The next Java data type is a byte which can have values from -128 to 127 that is 8-bit signed two’s complement integer. It is used in replacement of int or other integer data types so that memory can be saved if the value of the variable is in between the -128 to 127. The default variable value in a byte is zero. For example;

class ByteExample {
public static void main(String[] args) {

byte range;

range = 124;
System.out.println(range);

// Error code below. Why?
// range = 200
}
}

Output

124

Short

The short data type can store the variable data value from -32768 to 32767 (16-bit signed two’s complement integer). It is also used in lieu of integer data types to store memory within a certain range. The short’s default value is as well zero. For example;

class ShortExample {
public static void main(String[] args) {

short temperature;

temperature = -200;
System.out.println(temperature);

}
}

Output

-200

Int

In the int data type variable value from -231 to 231-1 (32-bit signed two’s complement integer) can be stored. If you are using Java 8 or above, then you can assign 32-bit integer with the maximum of zero and the maximum value of 1. The default value here is also zero. For example;

class IntExample {
public static void main(String[] args) {

int range = -4250000;
System.out.println(range);
}
}

Output

-4250000

Long

The long data type can store a value from -263 to 263-1 (64-bit signed two’s complement integer). For the Java 8 or above users, you have the option to insert 64-bit integer with the minimum value of 0 and the maximum value of 264-1. The default value is set on zero. For example;

class LongExample {
public static void main(String[] args) {

long range = -42332200000L;
System.out.println(range);
}
}

Output

-42332200000

Double

This data type is a double precision of 64-bit floating point. This data function should be never used to describe precise values such as currency. Its default value is 0.0. For example;

class DoubleExample {
public static void main(String[] args) {

double number = -42.3;
System.out.println(number);
}
}

Output

-42.3

Float

The float data type is a single precision with 32-bit floating point. It should never be used for precise values like currency. The default value is 0.0f. Example;

class FloatExample {
public static void main(String[] args) {

float number = -42.3f;
System.out.println(number);
}
}

Output

-42.3

Char

Char is a 16 bit Unicode character. The minimum value of the char data type is ‘\u0000’ (0). The maximum value of the char data type is ‘\uffff’. It’s default value is \u0000. For example;

class CharExample {
public static void main(String[] args) {

char letter = ‘\u0051’;
System.out.println(letter);
}
}

Output

Q

Well, that’s was wrap on Java Variables and Data Types, for more happening news stay connected!

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href=""> <abbr> <acronym> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Send a Message