Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Character Set

It includes alphabets, digits and special symbol that are used to


represent the information called as character set.

Constants
Constants is a variable which does not change the value once it has
been assigned called constants. There is no any in built constants in java.

If you want to make variable as constants then must use keyword final
(we will teach you final keyword in next section).

Example- final int a=5; //this value will not be changed.


Data Types

In java, based on the data type of a variable, operating system allocates the
memory and decides what can be stored in the reserved memory.

Therefore, by assigning different data types to variables we can store


integers, decimal numbers and characters.

There are two types of data type:

1. Primitive data types


2. Non-primitive data types

Primitive data type

There are eight primitive data types we have in java. These are already
defined and named by a keyword.
byte :

▪ It is generally used to store numbers.


▪ Minimum value we can store in byte is -128(27).
▪ Maximum value we can store in byte is 127(27-1).
▪ Default size 1 byte (8 bit).
▪ Default value is 0.
▪ Example byte a = 100, byte b = -100.

short :

▪ It is generally used to store numbers.


▪ Minimum value we can store in short is -32768 (215).
▪ Maximum value we can store in short is 32767(215-1).
▪ Default size 2 byte (16 bit).
▪ Default value is 0.
▪ Example short a = 10000, short b = -10000.

int :

▪ It is generally used to store comparatively large numbers.


▪ Minimum value we can store in int is - 2,147,483,648 (231).
▪ Maximum value we can store in int is 2,147,483,647 (231-1).
▪ Default size 4 byte(32 bit)
▪ Default value is 0.
▪ Example int a = 1000000, int b = -1000000.

long :

▪ It is generally used to store largest numbers.


▪ Minimum value we can store in long is - 9,223,372,036,854,775,808 (263).
▪ Maximum value we can store in long is 9,223,372,036,854,775,807(263-1).
▪ Default size 8 byte (64 bit)
▪ Default value is 0.
▪ Example long a = 1000000000L, long b = -1000000000L.

float :

▪ It is generally used to store single precision numbers (e.g 10.6)


▪ Default size 4 byte (32 bit)
▪ Default value is 0.0f.
▪ Example float f1 = 234.4f
▪ It is never used to store highly precise values. It can store values up to
6 decimal digits.

double :

▪ It is generally used to store double precision numbers (e.g 20.36)


▪ Default size 8 byte (64 bit)
▪ Default value is 0.0d.
▪ Example double d1 = 234.44
▪ It is used to store highly precise values up to 15 decimal digits

boolean :

▪ It is used to store value as true or false.


▪ Default size is 1 bit.
▪ Default value false.
▪ Example boolean b1 = true

char :

▪ It is used to store characters.


▪ Default size is 2 byte(16 bit).
▪ Minimum value we can store in char is ‘\u0000’.
▪ Maximum value we can store in char is ‘\uffff’
▪ Default value ’\u0000’
▪ Example char character = ‘A’

Non-primitive data types

Non-primitive data types include classes, interfaces and Arrays.

Example

String name = “velocity”;

Non primitive data types are also known as reference data types.

Default value for reference variables is null.


Difference between Primitive & Non-primitive data type

Primitive Non-Primitive
Primitive data types are keywords. Non-Primitive are classes,
interfaces.
Memory size is fixed. Memory size is not fixed.
It starts with lower case letters. It always starts with capital letters.

Keywords
Keywords are predefined words.

We cannot use keywords as variable or object name or class name.

Most commonly used keywords in java are:

class : used to create classes.

interface : it is used to create interfaces.

new : used to create object

void : used with method to create a method which doesn’t return anything.

switch : it is used to create a switch control statement .

case : it is used to create cases inside switch control statement.

for : it is used to create for loop.

do : is used to create do-while loop


while : it is used to create while or do while loop.

break : it is used to break the loop or switch statement.

continue : it is used to skip the current iteration of the loop and jump to
next iteration.

extends : it is used to achieve inheritance.

implements : it is also used to implement an interface.

null : null means nothing. Is used to indicate that reference variable is


referring to nothing. null does not mean it is empty.

package : it is used to declare packages in java.

import : it is used to import packages, classes, interfaces.

this : it is used to refer current object in method or constructor.

super : it is used to refer parent class object .

Interview Questions :
1. Tell me some java keywords you frequently use.
2. Which keyword is used to create an object?
3. What is void keyword?
4. What is null? Does it mean empty?
5. Which keyword is used to create a class?
6. Which keyword is used to make a variable as constant?

You might also like