0% found this document useful (0 votes)
4 views14 pages

Java-U-1-Lec-2-Notes

Uploaded by

mk5780381
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)
4 views14 pages

Java-U-1-Lec-2-Notes

Uploaded by

mk5780381
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/ 14

Unit-I Lecture-2

Today’s Target
 Data Types
 Primitive, Non-primitive
 Primitive Data Types
 Integer
 byte, short, int, long
 Floating-point
 float, double
 Character
 char
 Boolean
 boolean
Object Oriented Programming with Java

Data Types
“Data types specify which type of value a variable can hold in a programming language.”

There are two types of data types:

Primitive Data Types:


These are the basic data types in Java, representing single values like numbers, characters, and boolean
values. They are predefined by the language and include types such as int, char, boolean, etc. They are also
commonly referred to as simple types.

Non-Primitive Data Types:


These are more complex data types in Java, such as objects and arrays, which are derived from primitive
types and can hold multiple values or more intricate data structures. They are also called reference types.
Object Oriented Programming with Java

Data Types
Primitive Data Types:

Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.

These data types can be put in four groups:


1. Integers
This group includes byte, short, int, and long, which are for whole-valued signed numbers.
2. Floating-point numbers
This group includes float and double, which represent numbers with fractional precision.
3. Characters
This group includes char, which represents symbols in a character set, like letters and numbers.
4. Boolean
This group includes boolean, which is a special type for representing true/false values.
Object Oriented Programming with Java

Data Types
Primitive Data Types:
1. Integer Types
 Java provides four integer data types: byte, short, int, and long.
 All of these support both positive and negative values (signed integers).
 Unlike some other programming languages, Java does not include support for unsigned integers,
which store only non-negative values.
 The width and ranges of the integer types are given below:

Name Width Range


long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127
Object Oriented Programming with Java

Data Types
Primitive Data Types:
1. Integer Types

 byte: The byte is the smallest integer type which is useful when dealing with raw binary data. It is a
signed 8-bit type that has a range from –128 to 127.

 short: short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is the least used Java
type.

 int: The int type is the most commonly used integer type. It is a signed 32-bit type that has a range
from –2,147,483,648 to 2,147,483,647. int is often the best choice when an integer is needed as
even when byte and short values are used in an expression, they are promoted to int when the
expression is evaluated.

 long: The long type is a 64-bit signed integer, suitable for situations where an int cannot
accommodate the required value. Its extensive range makes it ideal for handling large whole
numbers.
Object Oriented Programming with Java

Data Types
Primitive Data Types:
1. Integers
Example
public class IntegerTypesDemo {
public static void main(String[] args) {
// 1. byte: Smallest integer type, range: -128 to 127
byte smallValue = 100;
System.out.println("Byte value: " + smallValue);

// 2. short: Larger than byte, range: -32,768 to 32,767


short mediumValue = 30000;
System.out.println("Short value: " + mediumValue);

// 3. int: Default integer type, range: -2,147,483,648 to 2,147,483,647


int normalValue = 1_000_000; // Underscores improve readability
System.out.println("Int value: " + normalValue);

// 4. long: For very large numbers,


// range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
long largeValue = 9_000_000_000L; // 'L' suffix is needed for long literals
System.out.println("Long value: " + largeValue);
}
}
Object Oriented Programming with Java

Data Types
Primitive Data Types:

2. Floating-point Types
 Java provides two floating-point data types: double, and float.
 Floating-point numbers, also known as real numbers, are used when evaluating expressions that
require fractional precision.
 float and double, represent single and double precision numbers, respectively.
 The width and ranges of the integer types are given below:

Name Width Range


double 64 4.9e–324 to 1.8e+308
float 32 1.4e–045 to 3.4e+038
Object Oriented Programming with Java

Data Types
Primitive Data Types:

2. Floating-point Types

 float: The float type is a 32-bit single-precision floating-point number. It can represent numbers with
fractional precision and has a range from 1.4e-45 to 3.4e+38. It is generally used when memory
efficiency is a concern and when high precision is not required.

 double: The double type is a 64-bit double-precision floating-point number. It has a much larger range
and higher precision compared to float, from 4.9e-324 to 1.8e+308. It is the default choice for
representing decimal values in Java and is widely used for scientific and mathematical computations.
Object Oriented Programming with Java

Data Types
Primitive Data Types:

2. Floating-point Types
Example:

public class FloatingPointTypesDemo {


public static void main(String[] args) {
// Declaring and initializing float and double variables

float floatValue = 1.123456789f; // 'f' is added to indicate it's a float value


double doubleValue = 1.123456789123456; // double has higher precision

System.out.println("Float value: " + floatValue);


System.out.println("Double value: " + doubleValue);
}
}
Object Oriented Programming with Java

Data Types
Primitive Data Types:

3. Character Type
 In Java, char is used to store characters.
 Java uses Unicode to represent a wide range of characters from various languages. It ensures global
portability
 A char is 16 bits wide, unlike C/C++ where char is typically 8 bits.
 The range of a char is from 0 to 65,536. char values in Java cannot be negative.
 Though Java uses Unicode for broader compatibility but ASCII (0-127) and ISO-Latin-1 (0-255)
character sets are also supported.
Object Oriented Programming with Java

Data Types
Primitive Data Types:

3. Character Type
Example:

class CharTypeDemo {
public static void main(String args[]) {
char ch1, ch2;

// Assigning numeric Unicode values


ch1 = 65; // ASCII code for 'A'
ch2 = 'B’; // Character 'B'

// Displaying the characters


System.out.print("ch1, and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
Object Oriented Programming with Java

Data Types
Primitive Data Types:

4. Boolean Type
 Java has a primitive type, called boolean, which can have only one of two possible values, true or
false.
 This is the type returned by all relational operators.
 boolean is also the type required by the conditional expressions that govern the control statements
such as if.
Object Oriented Programming with Java

Data Types
Primitive Data Types:

4. Boolean Type
Example:

class BooleanTypeDemo {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);

b = true;
// a boolean value can control the if statement
if (b)
System.out.println("Inside if.");

// outcome of a relational operator is a boolean value


System.out.println((8 > 9));
}
}

You might also like