0% found this document useful (0 votes)
2 views

JAVA

The document provides a comprehensive overview of Java programming concepts, including access modifiers, static methods, void return types, command-line arguments, primitive types, and their ranges. It also covers control flow statements such as if-else, switch, loops (for, while, do-while), and the differences between break and continue statements. Additionally, it explains method definitions, method overloading, parameters, and arguments, emphasizing code reusability and optimization.

Uploaded by

lakshmimahaa999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA

The document provides a comprehensive overview of Java programming concepts, including access modifiers, static methods, void return types, command-line arguments, primitive types, and their ranges. It also covers control flow statements such as if-else, switch, loops (for, while, do-while), and the differences between break and continue statements. Additionally, it explains method definitions, method overloading, parameters, and arguments, emphasizing code reusability and optimization.

Uploaded by

lakshmimahaa999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA

1. What are Access Modifiers?


2. What is meant by Static?
3. What is meant by void?
4. What are command-line-arguments?
5. Primitive Types and their usage along with wrapper classes.
6. Ranges of Primitive types
7. What is overflow and underflow?
8. What is casting?
9. If-then-else statement and terinary operator
10. Switch statement and Enhanced Switch statement
11. What are classes?
12. What are methods and how to define one?
13. What is method overloading?
14. What are parameters?
15. What are arguments?
16. For loop, while loop and do-while loop
17. What is the difference between continue and break?

1. What are Access Modifiers?

1.a) There are four types of Java access modifiers:

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

Decision-making in Java helps to write decision-driven statements and execute a particular


set of code based on certain conditions. The if statement alone tells us that if a condition is
true it will execute a block of statements and if the condition is false it won’t. In this article,
we will learn about Java if-else.
If-Else in Java
If- else together represents the set of Conditional statements in Java that are executed
according to the condition which is true.
Syntax of if-else Statement
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Java if-else Flowchart
if-else Program in Java
Dry-Run of if-else statements
1. Program starts.
2. i is initialized to 20.
3. if-condition is checked. 20<15, yields false.
4. flow enters the else block.
4.a) "i is greater than 15" is printed
5. "Outside if-else block" is printed.
Below is the implementation of the above statements:
Java
// Java program to illustrate if-else statement

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");

System.out.println("Outside if-else block");


}
}

Output
i is greater than 15
Outside if-else block

Trenary operator: few types:

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

Methods: Java Methods

Last Updated : 12 Aug, 2024


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:

For loop Do-While loop

Statement(s) is executed once the Condition is checked after the statement(s)


condition is checked. is executed.

It might be that statement(s) gets executed


Statement(s) is executed at least once.
zero times.

For the single statement, bracket is not


Brackets are always compulsory.
compulsory.

Initialization may be outside or in Initialization may be outside or within the


condition box. loop.

for loop is entry controlled loop. do-while is exit controlled loop.

for ( init ; condition ; iteration ) do { statement(s); }


{ statement (s); } while (condition);

 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)
}

o terminates marking the end of its life cycle.


 do while: do while loop is similar to while loop with only difference that it checks for
condition after executing the statements, and therefore is an example of Exit Control
Loop.
Syntax:
do
{
statements..
}
while (condition);
Flowchart:

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.

You might also like