Data Types and keywords in Java

Data Types and keywords in Java

What is a Data Type?

Data type is used to specify the type and their size of data,different languages have different sizes fo the different data types

The data types of Java are classified into two types

  1. Primitive Data type
  2. Non-Primitive Data type
Data Types and keywords in Java infograpics### Primitive Data type

Integer type data types

Typesize in bytesrangeDefault valueexample
int4–2,147,483,648 to 2,147,483,6470int i=32
short2–32,768 to 32,7670short s=11
long8–9,223,372,036,854,775,808 to9,223,372,036,854,775,8070long l=24235345
byte1–128 to 1270byte b=13

float type data types

Typesize in bytesrangeDefault valueexample
float4±3.40282347E+38F0.0ffloat f=123.10f
double8±1.79769313486231570E0.0ddouble d=12.10

decimal numbers are always classified into double, if user doest use character f .if the user uses suffix f with the value, then it is a float datatype

  • Q) Should i use suffix d in the value for double data type in java?

A) There is no difference ,you can use suffix d,or if you wont use the datatype is automatically taken as double

Characters datatype

Typesize in bytesrangeDefault valueexample
char20 to 65535\u0000 (0)char c=’a’

For char type the input value should in singe quotes ,like in the example above the value for c is a ,and this character a is between singles quotes ‘a’

Boolean datatype

a boolean data type can be declared using the below syntax, and the value can be either true or false, and this data type cannot be converted into other data types

boolean b; // Declares a boolean variable named done
b = true; // Assigns true to done

Non-Primitive Data type

  • Non-Primitive Data types are the data types which are called as refernce data types, beacuse they are used to refer objects
  • Non-Primitive Data types are not predifined data types,they are user defined by the user ,(string is the only Non-prmitive datatype which is defined by java)
  • These are used to refer the objects and they can be used to call methods ,from the class and perform some operations
  • While prmitive data tyes have some value,Non prmitive datatype can be assigned with a null .
  • Examples for Non-Primitive Data types are Strings, Classes, Arrays, Interface

Keywords in Java

Keywords are the reserved words which can be used to name any variable in the java program. these are predefine and have special meaning and does special task inside the program

abstractassertbooleanbreakbyte
casecatchcharclassconst
continuedefaultdodoubleelse
enumextendsfinalfinallyfloat
forgotoifimplementsimport
instanceofintinterfacelongnative
newpackageprivateprotectedpublic
returnshortstaticstrictfpsuper
switchsynchronizedthisthrowthrows
transienttryvoidvolatilewhile
Last updated on by vishal devxo