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

Java Chapter3 Notes

it provides detailed explanation for chapter3

Uploaded by

Madhu Gopinath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Java Chapter3 Notes

it provides detailed explanation for chapter3

Uploaded by

Madhu Gopinath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 3: Data Types, Variables, and

Arrays – Detailed Notes


Introduction
Java is a strongly typed language – every variable must be declared with a type.

Primitive data types form the foundation for all computations.

This chapter explains: primitive types (integers, floating-point, char, boolean), variables and
literals, type casting and promotion, arrays.

Strong Typing
Strong typing ensures that variables are used consistently and safely.

Type mismatches result in compile-time errors.

Primitive Types - Integers


Types: byte (8-bit), short (16-bit), int (32-bit), long (64-bit).

All are signed types; Java does not support unsigned (except char).

Range: byte = -128 to 127, short = -32,768 to 32,767, int = approx. ±2 billion, long = very
large range.

Program - Scope of Variables


class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if (x == 10) {
int y = 20; // known only within this block
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
System.out.println("x is " + x);
}
}

Explanation: Demonstrates scope of variables inside and outside blocks.


Primitive Types - Floating-Point
float (32-bit) and double (64-bit).

double is default for decimals in Java.

Example: double pi = 3.14159;

Primitive Types - Characters


char is 16-bit Unicode, unlike C/C++ which used 8-bit ASCII.

Example: char ch1 = 88; // code for 'X', char ch2 = 'Y';

Primitive Types - Booleans


Values: true or false.

Cannot be converted to/from integers.

Example: boolean b = true; if (b) System.out.println("This is


executed.");

Literals
Integer literals: decimal, octal (prefix 0), hex (prefix 0x).

Floating literals: must contain decimal point or exponent.

Character literals: single quotes, can include escape sequences (\n, \t).

String literals: enclosed in double quotes.

Variables
Declaration: int x;

Initialization: int x = 10;

Dynamic initialization: initialized at runtime using expressions.

Example: double a = 3.0, b = 4.0; double c = Math.sqrt(a*a + b*b);

Type Conversion and Casting


Widening conversion: automatic (e.g., int → long).

Narrowing conversion: explicit cast required.


Example:
int i = 257;
byte b = (byte) i; // b = 1 due to overflow.

Program - Automatic Type Promotion


class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}

Explanation: Demonstrates type promotions in expressions.

Arrays
Arrays are collections of variables of the same type.

Declared as: int month_days[] = new int[12];

Initialized as: int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Program - Array Example


class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}

Explanation: Demonstrates arrays and 0-based indexing.

Program - Two-Dimensional Array


class TwoDArray {
public static void main(String args[]) {
int twoD[][] = new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}

Explanation: Demonstrates initialization and traversal of 2D arrays.

Program - Ragged (Jagged) Arrays


class Ragged {
public static void main(String args[]) {
int riders[][] = new int[7][];
riders[0] = new int[10];
riders[1] = new int[10];
riders[2] = new int[10];
riders[3] = new int[10];
riders[4] = new int[10];
riders[5] = new int[2];
riders[6] = new int[2];
int i, j;
for(i=0; i<5; i++)
for(j=0; j<10; j++)
riders[i][j] = i + j + 10;
for(i=5; i<7; i++)
for(j=0; j<2; j++)
riders[i][j] = i + j + 10;
for(i=0; i<7; i++) {
for(j=0; j<riders[i].length; j++)
System.out.print(riders[i][j] + " ");
System.out.println();
}
}
}

Explanation: Demonstrates jagged arrays where rows have different lengths.

Summary
Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.

Strong typing prevents unsafe operations.

Covers literals, variables, casting, promotion rules, and arrays (1D, 2D, jagged).

Programs demonstrate variable scope, promotions, and array handling.

You might also like