0% found this document useful (0 votes)
8 views15 pages

Java Notes Basics 1770972586

The document provides an overview of Java basics, covering data types, variables, operators, control statements like switch and loops, and methods. It explains the definitions, syntax, pros, and cons of each concept, emphasizing the importance of type safety, code reusability, and modular programming. Additionally, it includes examples to illustrate the usage of various Java constructs.

Uploaded by

priyaraju120505
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)
8 views15 pages

Java Notes Basics 1770972586

The document provides an overview of Java basics, covering data types, variables, operators, control statements like switch and loops, and methods. It explains the definitions, syntax, pros, and cons of each concept, emphasizing the importance of type safety, code reusability, and modular programming. Additionally, it includes examples to illustrate the usage of various Java constructs.

Uploaded by

priyaraju120505
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

JAVA BASICS –NOTES

1. DATA TYPES IN JAVA


Definition

Data types specify the type of data a variable can store. Java is a strongly typed
language, so every variable must have a data type.

Explanation

Data types help Java allocate the correct memory size and prevent storing invalid values
in variables.

Types

Primitive Data Types:


byte, short, int, long, float, double, char, boolean

Non-Primitive Data Types:


String, Array, Class, Object, Interface

Syntax

int age = 20;

Example

double salary = 45000.75;

char grade = 'A';

boolean isPassed = true;

Uses

• Memory allocation

• Type safety

• Faster execution

Pros

• Prevents invalid data storage

• Improves performance
Cons

*primitive data types have fixed size

2. VARIABLES IN JAVA
Definition

Variables are containers used to store data values during program execution.

Explanation

The value of a variable can change while the program runs.

Types of Variables

1. Local Variable – declared inside a method

2. Instance Variable – declared inside a class

3. Static Variable – shared among all objects

Syntax

datatype variableName = value;

Example

String language = "Java";

int count = 5;

Uses

• Store data temporarily

• Perform calculations

• Control program logic

Pros

• Makes programs dynamic

• Easy data manipulation

Cons

• Incorrect scope usage causes errors


3. OPERATORS (BASICS)
Definition

Operators are symbols used to perform operations on variables and values.

Types

• Arithmetic: + - * / %

• Relational: > < >= <= == !=

• Logical: && || !

• Assignment: = += -=

• Unary: ++ --

Example

int sum = a + b;

boolean result = a > b;

Uses

• Calculations

• Comparisons

• Decision making

Pros

• Simplifies operations

Cons

• Misuse leads to logical errors

4. SWITCH STATEMENT
Definition

The switch statement executes one block of code from multiple options based on a
given value.

Explanation

It is an alternative to long if-else chains and improves readability.


Syntax

switch(expression) {

case value1:

statements;

break;

default:

statements;

Example

int day = 1;

switch(day) {

case 1:

[Link]("Monday");

break;

default:

[Link]("Invalid day");

Uses

• Menu-driven programs

• Fixed choice conditions

Pros

• Clean and readable

• Faster than multiple if-else

Cons

• Works only with specific data types

• Requires break to avoid fall-through


5. ENHANCED SWITCH STATEMENT (Java 12+)
Definition

Enhanced switch is a modern version of the switch statement with simpler syntax.

Explanation

It removes the need for break and prevents fall-through errors.

Syntax

switch(day) {

case 1 -> [Link]("Monday");

default -> [Link]("Invalid");

Uses

• Modern Java applications

• Cleaner code structure

Pros

• Less code

• More readable

• Safer

Cons

• Requires newer Java versions


6. LOOPS IN JAVA
Definition

Loops are used to execute a block of code repeatedly until a condition becomes false.

a) FOR LOOP
Explanation: Used when the number of iterations is known.

Syntax

for(initialization; condition; increment) {

statements;

Example

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

[Link](i);

Pros: Compact and fast


Cons: Less flexible

b) WHILE LOOP
Explanation: Used when the number of iterations is unknown.

Syntax

while(condition) {

statements;

Example

int i = 1;

while(i <= 5) {
[Link](i);

i++;

Pros: Flexible
Cons: Risk of infinite loop

c) DO-WHILE LOOP
Explanation: 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);

Pros: Guaranteed execution


Cons: May execute unnecessarily once

7. FOR-EACH LOOP
Definition

For-each loop is used to iterate over arrays and collections easily.

Syntax

for(datatype variable : array) {

statements;

}
Example

int[] nums = {1, 2, 3};

for(int n : nums) {

[Link](n);

Uses

• Traversing arrays

• Cleaner iteration

Pros

• Simple and readable

• No index handling

Cons

• Cannot modify index

• Not suitable when index is needed

1. WHAT IS A FUNCTION (METHOD)?


Definition

A function (called a method in Java) is a block of code that performs a specific task and
can be reused whenever needed.

Explanation

Instead of writing the same code again and again, we place it inside a method and call it
whenever required. This makes the program clean, reusable, and easy to maintain.

2. GENERAL SYNTAX OF A METHOD


returnType methodName(parameters) {

// method body

• returnType → specifies what value the method returns

• methodName → name of the method


• parameters → values passed to the method (optional)

3. FUNCTION WITHOUT PARAMETERS AND WITHOUT


RETURN TYPE
Definition

This type of method does not take any input and does not return any value.

Explanation

It simply performs a task when called.

Syntax

void methodName() {

statements;

Example

static void greet() {

[Link]("Welcome to Java");

Method Call

greet();

Uses

• Display messages

• Simple operations

Pros

• Easy to write and understand

• No input/output handling

Cons

• Cannot reuse logic with different values


4. FUNCTION WITH PARAMETERS AND WITHOUT
RETURN TYPE
Definition

This method accepts input values (parameters) but does not return any value.

Explanation

The method performs operations using the given inputs.

Syntax

void methodName(datatype param1, datatype param2) {

statements;

Example

static void add(int a, int b) {

[Link](a + b);

Method Call

add(10, 20);

Uses

• Performing actions using user input

• Printing calculated results

Pros

• More flexible than no-parameter methods

• Accepts dynamic values

Cons

• Result cannot be reused outside the method


5. FUNCTION WITHOUT PARAMETERS AND WITH
RETURN TYPE
Definition

This method does not take any input but returns a value.

Explanation

The method calculates something internally and returns the result.

Syntax

returnType methodName() {

return value;

Example

static int getNumber() {

return 10;

Method Call

int num = getNumber();

Uses

• Returning fixed or pre-calculated values

Pros

• Result can be reused

• Clean and efficient

Cons

• Cannot accept external input


6. FUNCTION WITH PARAMETERS AND WITH RETURN
TYPE (MOST IMPORTANT)
Definition

This method accepts input values and returns a result.

Explanation

It is the most commonly used type of method in real-world Java programs.

Syntax

returnType methodName(datatype param1, datatype param2) {

return value;

Example

static int add(int a, int b) {

return a + b;

Method Call

int result = add(5, 3);

Uses

• Calculations

• Business logic

• Reusable operations

Pros

• Highly reusable

• Flexible and efficient

Cons

• Slightly complex for beginners


7. RETURN TYPE
Definition

Return type specifies what type of value a method sends back to the caller.

a) void Return Type

• Returns nothing

• Used when output is not required

static void show() {

[Link]("Hello");

b) Non-Void Return Type

• Returns a value (int, double, String, etc.)

• Must use the return keyword

static String getName() {

return "Java";

8. IMPORTANT RULES OF RETURN STATEMENT


• return must match the return type

• Code after return will not execute

• void methods cannot return values


9. COMPARISON TABLE
Type Parameters Return Type Example

No param, no return void void show()

Param, no return void void add(int a)

No param, return int get()

int add(int a,int


Param, return
b)

10. WHY FUNCTIONS ARE IMPORTANT


• Code reusability

• Easy debugging

• Better readability

• Modular programming

Keywords
Reserved words like int, class, static, return

Identifiers
Names of variables, methods, and classes

Comments
// Single-line comment

/* Multi-line comment */

Constants
Declared using final

final int MAX = 100;

You might also like