0% found this document useful (0 votes)
9 views3 pages

Values and Data types-SM - 2

The document discusses data types in Java, categorizing them into primitive (e.g., int, char, double) and non-primitive (e.g., class, array, String) types. It details the size and format of various primitive data types and explains type conversion, including implicit (automatic) and explicit (manual) conversions. Additionally, it provides sample questions related to the content covered in the document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Values and Data types-SM - 2

The document discusses data types in Java, categorizing them into primitive (e.g., int, char, double) and non-primitive (e.g., class, array, String) types. It details the size and format of various primitive data types and explains type conversion, including implicit (automatic) and explicit (manual) conversions. Additionally, it provides sample questions related to the content covered in the document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Values and Data types(Part-II)

Data types:
Data types are used to identify the type of value that will be stored in an identifier.
Data types in Java:
1. Primitive data types: These are the fundamental built in data types that are independent of any other data
type.
Example: int , char , double , boolean etc.
2. Non-primitive: These are data types are derived data types which are dependent on primitive data types.
Example: class , array , String etc.

Table of primitive data types :


Data type Size Format

byte 1 byte /8 bits byte age;

short 2 bytes/16 bits short sales;

int 4 bytes /32 bits int balance;

long 8 bytes /64 bits long dist=54582;

float 4 bytes/32bits float a;

double 8 bytes /64 bits double b=54.582;

char 2 bytes /16 bits char a=‘A’;

boolean 1 bit boolean f=true;

NOTE: In Java double is the default floating point data type.


Type conversion
The process of converting one pre-defined data type to another is called Type conversion.

Type conversion

Implicit Explicit

Implicit conversion /Coercion /Widening: is done automatically by the compiler when the data types are
compatible with each other and the smaller data type is promoted into higher data type. This is called as Type
promotion. But no widening conversion is supported from numeric type to char or boolean because the
numeric type is incompatible with char or boolean. Also char and boolean are incompatible with each other.
The following conversion takes place implicitly:
byte ->short -> int ->long ->float ->double
char ->int ->long ->float ->double
Example: char c=’A’;
int x= c;
Explicit conversion/Type casting: is done forcefully by the user using (type) operator where the higher data
type is converted to smaller data type.
General form : (typename)value

Example: double x= 2.5;


int y=(int) x;

SAMPLE QUESTIONS:

1. Name the two types of data types in Java.


2. What is implicit type conversion?
3. What is type casting?
4. Write the size of double data type in terms of bits and bytes.
5. What are the types of conversion shown by the following examples?
i) char c= (char)120;
ii) int x= 't’;
6. Name any two non-primitive data type.
7. Arrange the data types in ascending order according to their size:
double, char , int , byte .

You might also like