Java Data types

data types

Data types in Java

Java supports several types of data , you can use these types to declare variables and to create arrays. Data types in Java are also referred as simple types or primitive types. Java has divided the types into two categories predefined types and referenced types. Predefined types are nothing but already existing types in Java like int , float , char etc.  Referenced types are  classes , interfaces and array types  , for example ‘class’  Creating a ‘class’ is like creating a new data type and you can use this type to create object. Well this can be clearly explained in the classes and methods chapter.

 

Primitive types

Primitive types are predefined type in Java programming language. Under primitive type you can observe several data types to declare a variable. Primitive type are put into four groups.

Integers : byte , short , int and long.

Floating-point numbers :  float and double , which represent numbers with fractional precision

Characters : This group includes char which represents symbols in a character set, like letters and symbols.

Boolean : This group includes boolean , which is a special type for representing true/false values.

All the data types under primitive had strictly range  , for example int is always 32 bits regardless of the platform.

Integers

  • Java defines four types of integers : byte , short , int and long. All of these are signed , positive and negative values.
  • The width and ranges of these integers types vary widely , as shown in the below table
data types integer
Data types

Byte

  • Byte variables are declared by the used byte keyword. For example byte b,c;
  • The smallest integer type is byte , is a signed 8-bit type that has a range from -128 to +128.
  • Variables of type byte are especially used when you’re working with network values.

Short

  • Short variables are declared by the use of short keyword for example short s;
  • Short is 16-bit signed type , it is probably the least used Java type

int

  • The most commonly used integer type is int , it is signed 32-bit type has a range from -2,147,483,648 to  +2,147,483,648.
  • Variables of type int are commonly used in loops and to index an array.
  •  Although you might think that using a byte or short would be more efficient than using an int in situations. But Java always prefers an int type for portability and type casting which is explained down the line.

long

  • long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.
  • The range of long is quite large , this makes it useful when whole and large numbers are needed to store in a variable.
  • For example int cannot hold the speed of the light in number , where long can hold. Below is the program which explains where long type.

data types long

  • As you can see the output ,the value “16070400000000” holding by the distance variable of type long , where int cannot hold such a large number.
  • Type casting is required to hold by the int type .

 

Floating-point types

  • Floating-point types are also known are real numbers , which are used when evaluating an expression which contains decimal at the end of the number.
  • For example calculating the value of sine or cosine , might require the floating-point type in order hold the value , because the values of sine or cosine may contain fractional precision(decimal).
  • There are two kinds of floating-point type one is float and the other is double .
  • Width and range of these two types are shown below
data types floating point
Data types Floating-point type

float

  • The type float specifies single decimal value that uses 32-bit of storage.
  • float uses half of double type , which helps in processing fast results.
  • Variables of type float are useful when you need a fractional component , but don’t require a large degree of decimals.

double

  • Type double uses 64-bit to store a double decimal value.
  • double precision is actually faster than single precision on some modern processors that have been optimized for high-sped mathematical calculations.
  • All  transcendental math functions such as sin() , cos() returns double values.
  • when you need accuracy for any calculation in a program it’s better to use double type
  • Example : double pi =3.1416 which is the value of pi .

 

Characters

  • In Java , the data type used for characters is char . The char in C/C++ is different from char in Java.
  • In C/C++ the char is 8 bits where as in Java the storage space is 16-bits.
  • Java uses unicode to represent characters. For example the value of character ‘x’ is 88
  • Unicode defines a fully international character set that can represent all of the characters found in the human language.  For this purpose it requires 16-bits of space.
  • Unicode ranges from 0 to 127, unicode which is also represented as ASCII. The ASCII value of ‘x’ is 88.
  • The range of char is from 0 to 65,536 , there are no negative chars.
  • Although char is used to hold ASCII values , it can also be used as an integer type on which you can perform arithmetic operations.

data types char

Booleans

  • In Java it is possible to store logical values true/false in a variable .
  • It is done through using the primitive data type called  boolean. Boolean can have only two possible values that is true or false.
  • Boolean will be mostly used in the control statements like  ‘if’ and ‘for’.
  • To control a statement boolean type is mostly used , for example below is the program that demonstrates the boolean type.

data types boolean

  • As you can observe in the above program , there are two control ‘if’ statements.
  • In Java if(b==true) is equal to if(b) , because b is already a boolean variable.
  • we will look into the output how it shows the values of b .

Output : 

data types boolean

We have to notice three points in the above program.

  • The first ‘if’ statement displays true  because , the last value stored in b is true. ‘if’ statements only displays when the condition is true. here the condition is ‘b==true
  • The second ‘if‘ statement failed to execute because , we changed the b value to false, ‘if’ statement only executes when the condition  is true.
  • Third , the outcome of the relational operator statement  “System.out.println(” 10>9 is “ + (10>9)); ” , this statement returned ‘true‘ because the relational operator we have used in the statement is “<” is a boolean value which returns either true or false depending on the operation.
  • In our case we stated (10<9) which is true , so it displayed true in the output.

5 thoughts on “Java Data types

  1. Hello there! I could have sworn I’ve been to this website before but after reading through some of the post I realized it’s new to me. Anyhow, I’m definitely glad I found it and I’ll be bookmarking and checking back often!

  2. Excellent post. I used to be checking continuously this weblog and I’m inspired! Extremely helpful info specifically the last section 🙂 I take care of such info much. I used to be looking for this particular information for a long time. Thank you and best of luck.

Leave a Reply

Your email address will not be published. Required fields are marked *