JAVA
JAVA
Private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.
There are many non-access modifiers, such as static, abstract, synchronized, native, volatile,
transient, etc. Here, we are going to learn the access modifiers only.
Void: A method without any return values: is void
Static: A static method can be accessed without creating an object of the class first:
Command line argument: The java command-line argument is an argument i.e. passed at the time
of running the java program.
The arguments passed from the console can be received in the java program and it can be
used as an input.
Primitive datatypes: Primitive data types in Java are predefined by the Java language and
named as the reserved keywords
1. Boolean data type
2. byte data type
3. int data type
4. long data type
5. float data type
6. double data type
7. char data type
8. short data type
overflow and underflow are issues that occur when a value exceeds the storage capacity of
a data type, or goes below its minimum value, respectively. Here’s a deeper look into these
concepts:
1. Overflow
Overflow occurs when a value exceeds the maximum limit that can be stored in a data type.
For example:
Integer Overflow: Java's int data type has a maximum value of 2,147,483,647 (i.e.,
2^31 - 1). If you add 1 to this value, it will wrap around to the minimum value of -
2,147,483,648 (i.e., -2^31).
int maxInt = Integer.MAX_VALUE; // 2147483647
int overflowedInt = maxInt + 1; // -2147483648
Floating-Point Overflow: For floating-point numbers (float and double), overflow
happens when a number exceeds the largest representable finite value. Java's float
and double types use IEEE 754 standard, so they will represent an overflow with
Infinity.
float largeFloat = Float.MAX_VALUE;
float overflowedFloat = largeFloat * 2; // Infinity
2. Underflow
Underflow happens when a value falls below the minimum limit that can be stored in a data
type. This is commonly discussed in the context of floating-point numbers:
Integer Underflow: For integers, underflow occurs when a value is less than the
minimum representable value. For example, subtracting 1 from Integer.MIN_VALUE
(i.e., -2,147,483,648) wraps around to the maximum value.
int minInt = Integer.MIN_VALUE; // -2147483648
int underflowedInt = minInt - 1; // 2147483647
Floating-Point Underflow: In floating-point types, underflow refers to a value
becoming so small that it is rounded to zero. Java represents these values using
denormalized numbers or zero.
double smallDouble = Double.MIN_VALUE; // 4.9e-324
double underflowedDouble = smallDouble / 2; // 0.0
casting:
Casting means to change one state to another state and is done by the programmer using
the cast operator. Type Casting is done during the program design time by the programmer.
Typecasting also refers to Narrow Conversion. B
If then else:
Java if-else
Last Updated : 02 Apr, 2024
class IfElseDemo {
public static void main(String args[])
{
int i = 20;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
Output
i is greater than 15
Outside if-else block
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift operator
Switch statement:
The switch statement in Java is a multi-way branch statement. In simple words, the Java
switch statement executes one statement from multiple conditions.
Enhanced statement:
m "enhanced statement" often refers to the enhanced for loop, which is also known as the
for-each loop. This loop provides a simpler way to iterate over arrays and collections without
using an iterator or managing indices manually.
. it is used to execte the block of statements repatedly
The method in Java or Methods of Java is a collection of statements that perform some
specific tasks and return the result to the caller. A Java method can perform some specific
tasks without returning anything. Java Methods allows us to reuse the code without retyping
the code. In Java, every method must be part of some class that is different from languages
like C, C++, and Python.
A method is like a function i.e. used to expose the behavior of an object.
It is a set of codes that perform a particular task.
Syntax of Method:
<access_modifier> <return_type> <method_name>( list_of_parameters)
{
//body
}
Advantage of Method:
Code Reusability
Code Optimization
Note: Methods are time savers and help us to reuse the code without retyping the code.
1. Defining Methods
A method in Java is defined within a class and has the following components:
Method Signature: Includes the method name and parameters (if any).
Return Type: The type of value the method returns. If the method does not return
any value, the return type is void.
Method Body: The block of code that defines what the method does, enclosed in
curly braces {}.
Syntax
java
Copy code
returnType methodName(parameterList) {
// Method body
// Code to execute
return value; // If returnType is not void
}
method overloading: if a class has multiple methods having same name but different
in parameter.
Advantage: 1. Clean lines of code
2, flexibility
Argument
An argument is a value passed to a function when the function is called. Whenever any
function is called during the execution of the program there are some values passed with the
function. These values are called arguments.
Parameters
A parameter is a variable used to define a particular value during a function definition.
Whenever we define a function we introduce our compiler with some variables that are
being used in the running of that function. These variables are often termed as Parameters.
Here is the difference table:
while loop: A while loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The while loop can be thought of as
a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
Flowchart:
Flowchart:
for loop:
Loops in Java come into use when we need to repeatedly execute a block of
statements.
for loop provides a concise way of writing the loop structure. Unlike a while loop, a
for statement consumes the initialization, condition and increment/decrement in one
line thereby providing a shorter, easy to debug structure of looping.
Syntax:
for (initialization condition; testing condition;increment/decrement)
{
statement(s)
}
Break: The break statement in java is used to terminate from the loop immediately. When a
break statement is encountered inside a loop, the loop iteration stops there, and control
returns from the loop immediately to the first statement after the loop. Basically, break
statements are used in situations when we are not sure about the actual number of iteration
for the loop, or we want to terminate the loop based on some condition.
Syntax :
break;
Continue:
The continue statement in Java is used to skip the current iteration of a loop. We can use
continue statement inside any types of loops such as for, while, and do-while loop. Basically
continue statements are used in the situations when we want to continue the loop but do
not want the remaining statement after the continue statement.
Syntax:
continue;
Using continue to continue a loop
Using continue, we can skip the current iteration of a loop and jumps to the next iteration of
the loop immediately.