[Go to site: main page, start]

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

Java DataTypes Notes

The document provides an overview of Java programming concepts, including data types, variables, operators, conditional statements, loops, arrays, and methods. It explains primitive and non-primitive data types, type casting, various operators, and control flow structures with examples. Additionally, it covers method creation, overloading, and recursion.

Uploaded by

dinip84558
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)
4 views21 pages

Java DataTypes Notes

The document provides an overview of Java programming concepts, including data types, variables, operators, conditional statements, loops, arrays, and methods. It explains primitive and non-primitive data types, type casting, various operators, and control flow structures with examples. Additionally, it covers method creation, overloading, and recursion.

Uploaded by

dinip84558
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

1.

Data Types and Variables

Data Types

A data type specifies the type of data a variable can store. Java is a statically typed
language, meaning variables must be declared with a data type before use.

Variables

A variable is a named memory location used to store data during program execution.

Syntax:

dataType variableName = value;

Example:

int age = 20;double salary = 45000.50;char grade = 'A';

2. Primitive Data Types

Primitive data types are basic built-in types that store single values. Java has 8
primitive data types.

Data
Size Description Example
Type

byte 1 byte Small integer byte b = 10;

short 2 bytes Short integer short s = 200;

int 4 bytes Integer int x = 1000;

long 8 bytes Large integer long l = 900000L;

Decimal (single
float 4 bytes float f = 5.5f;
precision)

Decimal (double
double 8 bytes double d = 10.75;
precision)

char 2 bytes Single character char c = 'A';


Data
Size Description Example
Type

boolean 1 bit True/False boolean flag = true;

Example Program:

int number = 10;float price = 99.99f;char letter = 'J';boolean


isJavaFun = true;

3. Non-Primitive Data Type: String

A String is a non-primitive (reference) data type used to store a sequence of


characters.
In Java, String is a class, not a primitive type.

Characteristics of String

Immutable (cannot be changed once created)

Stored as an object

Supports many built-in methods

Declaration:

String name = "Java Programming";

Example:

String city = "Delhi";

[Link](city);

[Link]([Link]());

4. Implicit Type Casting (Widening)

Implicit type casting occurs when:


Smaller data type is converted into a larger data type

Done automatically by Java

No data loss

Order:
byte → short → int → long → float → double

Example:

int a = 100;double b = a; // implicit casting

[Link](b);

Another Example:

float f = 10.5f;double d = f;

5. Explicit Type Casting (Narrowing)

Explicit type casting occurs when:

Larger data type is converted into a smaller data type

Must be done manually

May cause data loss

Syntax:

(targetType) value;

Example:

double x = 100.75;int y = (int) x;

[Link](y); // Output: 100


Another Example:

int a = 130;byte b = (byte) a;

[Link](b); // Output: -126 (data loss)

Difference: Implicit vs Explicit Type Casting


Implicit
Explicit Casting
Casting

Automatic Manual

No data loss Possible data loss

Smaller →
Larger → Smaller
Larger

Safe Risky

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operat
Meaning
or

+ Addition

- Subtraction

* Multiplication

/ Division

Modulus
%
(remainder)

Example:

int a = 10, b = 3;
[Link](a + b); // 13

[Link](a - b); // 7

[Link](a * b); // 30

[Link](a / b); // 3

[Link](a % b); // 1

2. Unary Operators

Unary operators operate on a single operand.

Operat
Meaning
or

+ Unary plus

Unary
-
minus

++ Increment

-- Decrement

Logical
!
NOT

Example:

int x = 5;

[Link](++x); // 6

[Link](--x); // 5

boolean flag = true;

[Link](!flag); // false

3. Bitwise Logic Operators


Bitwise operators work on binary bits of integers.

Operat
Meaning
or

Bitwise
&
AND

Bitwise
^
XOR

Bitwise
~
NOT

Example:

int a = 5; // 0101

int b = 3; // 0011

[Link](a & b); // 1

[Link](a | b); // 7

[Link](a ^ b); // 6

4. Bitwise Shift Operators

Shift operators shift bits left or right.

Operat
Meaning
or

<< Left shift

>> Right shift

>>> Unsigned right shift

Example:
int x = 8; // 1000

[Link](x << 1); // 16

[Link](x >> 1); // 4

5. Comparison (Relational) Operators

Used to compare two values and return a boolean result.

Operat
Meaning
or

== Equal to

!= Not equal

> Greater than

< Less than

Greater than or
>=
equal

<= Less than or equal

Example:

int a = 10, b = 20;

[Link](a > b); // false

[Link](a == b); // false

[Link](a != b); // true

6. Logical Operators

Logical operators operate on boolean values.


Operat
Meaning
or

Logical
&&
AND

Logical
!
NOT

Example:

int age = 25;boolean hasID = true;

[Link](age > 18 && hasID); // true

7. Assignment Operators

Used to assign values to variables.

Operat Examp
or le

= a = 10

+= a += 5

-= a -= 5

*= a *= 2

/= a /= 2

%= a %= 2

Example:

int a = 10;
a += 5;

[Link](a); // 15

8. Ternary (Conditional) Operator

The ternary operator is a short form of if-else.

Syntax:

condition ? value1 : value2;

Example:

int a = 10, b = 20;int max = (a > b) ? a : b;

[Link](max); // 20

9. Operator Precedence

Operator precedence determines the order of execution.

Precedence Order (High → Low)

1. Unary (++, --, !)


2. Arithmetic (*, /, %)
3. Arithmetic (+, -)
4. Shift (<<, >>)
5. Relational (<, >, <=, >=)
6. Equality (==, !=)
7. Logical AND (&&)
8. Logical OR (||)
9. Assignment (=, +=, etc.)
10. Ternary (?:)

Example:

int result = 10 + 5 * 2;

[Link](result); // 20
Conditional Statements & Methods

1. Conditional Flow Control

Conditional statements are used to execute different blocks of code based on a given
condition.

1.1 If Statement

The if statement executes a block of code when the condition is true.

Syntax:

if (condition) {

// statements

Example:

int age = 20;

if (age >= 18) {

[Link]("Eligible to vote");

Output:

Eligible to vote

1.2 Nested If Statement

A nested if statement is an if statement inside another if statement.

Syntax:
if (condition1) {

if (condition2) {

// statements

Example:

int marks = 85;

if (marks >= 35) {

if (marks >= 75) {

[Link]("Distinction");

Output:

Distinction

1.3 Switch Case Statement

The switch statement selects one execution path from multiple options.

Syntax:

switch(expression) {

case value1:

statements;

break;
case value2:

statements;

break;

default:

statements;

Example:

int day = 2;

switch (day) {

case 1:

[Link]("Monday");

break;

case 2:

[Link]("Tuesday");

break;

default:

[Link]("Invalid day");

Output:

Tuesday
2. Loops

Loops are used to execute a block of code repeatedly until a condition is met.

2.1 For Loop

The for loop is used when the number of iterations is known.

Syntax:

for (initialization; condition; increment/decrement) {

// statements

Example:

for (int i = 1; i <= 5; i++) {

[Link](i);

Output:

2.2 While Loop

The while loop executes as long as the condition is true.


Syntax:

while (condition) {

// statements

Example:

int i = 1;

while (i <= 5) {

[Link](i);

i++;

Output:

2.3 Do-While Loop

The do-while loop executes at least once even if the condition is false.

Syntax:

do {

// statements
} while (condition);

Example:

int i = 1;

do {

[Link](i);

i++;

} while (i <= 5);

Output:

2.4 Infinite Loop

An infinite loop runs forever unless stopped manually.

Example:

while (true) {

[Link]("Infinite Loop");

Output:
Infinite Loop

Infinite Loop

Infinite Loop

...

2.5 Break and Continue Statements

break terminates the loop.

continue skips the current iteration.

Example:

for (int i = 1; i <= 5; i++) {

if (i == 3)

break;

[Link](i);

Output:

Continue Example:

for (int i = 1; i <= 5; i++) {

if (i == 3)

continue;
[Link](i);

Output:

2.6 Nested Loop

A loop inside another loop is called a nested loop.

Example:

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 3; j++) {

[Link](i + " " + j);

Output:

11

12

13

21
22

23

31

32

33

2.7 Labeled Loop

Labels are used to control nested loops.

Example:

outer:for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 3; j++) {

if (i == 2)

break outer;

[Link](i + " " + j);

Output:

11

12

13
3. Arrays and Methods

3.1 Array

An array is a collection of elements of the same data type.

Syntax:

dataType[] arrayName = new dataType[size];

Example:

int[] numbers = {10, 20, 30, 40};

for (int i = 0; i < [Link]; i++) {

[Link](numbers[i]);

Output:

10

20

30

40

4. Methods

A method is a block of code that performs a specific task.

4.1 Creating a Method

Syntax:
returnType methodName(parameters) {

// statements

Example:

static void display() {

[Link]("Hello Java");

4.2 Calling a Method

Example:

display();

Output:

Hello Java

4.3 Method Overloading

Method overloading allows multiple methods with the same name but different
parameters.

Example:

static int add(int a, int b) {

return a + b;

}
static int add(int a, int b, int c) {

return a + b + c;

[Link](add(10, 20));

[Link](add(10, 20, 30));

Output:

30

4.4 Recursive Methods

A recursive method is a method that calls itself.

Example:

static int factorial(int n) {

if (n == 1)

return 1;

else

return n * factorial(n - 1);

Output:

120

You might also like