Java-U-1-Lec-2-Notes
Java-U-1-Lec-2-Notes
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.”
Data Types
Primitive Data Types:
Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.
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:
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);
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:
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:
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;
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.");