Java Basic Datatypes

Published by Admin on

Java Datatypes specify the values and different sizes that can be stored in the variable.

Two types of data types in Java :-

  1. Primitive data types : Eight primitive data types available in java. The primitive data types include byte, short, int, long, float and double, boolean, char.
  2. Non-primitive data types : The non-primitive data types include Arrays, Classes, And Interfaces.

Two data types available in Java :−

  • Primitive Data Types
  • Reference/Object Data Types

Primitive Data Types

There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and are designated by a keyword. Let us now look into the eight primitive data types size, range and default values.

TypeDefault
Size
RangeDefault ValueExample
byte1 byte-128 to 1270byte a = 10, byte b = -20
short2 bytes-32,768 to 32,7670short a = 10000, short b = -5000
int4 bytes-2,147,483,648 to 2,147,483, 6470int a = 100000, int b = -200000
long8 bytes-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
0long a = 100000L, long b = -200000L
float4 bytesapproximately ±3.40282347E+38F
(6-7 significant decimal digits)
Java implements IEEE 754 standard
0.0ffloat a = 234.5f
double8 bytesapproximately ±1.79769313486231570E+308
(15 significant decimal digits)
0.0ddouble a = 12.3
char2 byte0 to 65,536 (unsigned)‘\u0000’char letterA = ‘A’
boolean1 bittrue or falsefalseboolean flag = false

Reference/Object Datatypes

  • Reference variables are created using defined constructors of classes. They are used to access objects. These variables are declared to be of a particular type which cannot be changed. For example, Employee, Puppy, etc.
  • Class objects and variety of array variables come under reference datatype.
  • Default value of any reference variable is null.
  • It can be used to refer any object of the declared type or any compatible type.
  • Example: Animal animal = new Animal(“giraffe”);

Escape Sequences

Java language supports few special escape sequences for char and String literals as well. They are −

Escape SequencesCharacter represented
\nNewline (0x0a)
\rCarriage return (0x0d)
\fFormfeed (0x0c)
\bBackspace (0x08)
\sSpace (0x20)
\ttab
\”Double quote
\’Single quote
\\backslash
\dddOctal character (ddd)
\uxxxxHexadecimal UNICODE character (xxxx)

Question :-

  • what are Primitive data type in java?
  • What are long data type in Java?
  • How does Java pass primitive variables to methods – by value or by reference?


0 Comments

Leave a Reply

LinkedIn
LinkedIn
Share
Instagram
Follow by Email
YouTube
YouTube
Telegram
RSS