java Keyword:
Examples of Java keywords include:
Data Types: int, boolean, char, double, float, long, byte, short
Control Flow: if, else, switch, case, default, for, while, do, break, continue
Class and Object related: class, new, this, super, extends, implements, interface, abstract, final,
static, instanceof, enum, record, sealed, non-sealed, permits
Access Modifiers: public, private, protected
Exception Handling: try, catch, finally, throw, throws
Other: void, return, package, import, transient, volatile, synchronized, native, strictfp, assert
How to Execute java File in offline using IDE(Notepad++, Visule Studio
Code(vs), eclipse, Netbeans):
Open IDE(Notepad++) -->type the Source code(Program)-->Save--> compiler
--> run
How to Save java file:
open our IDE --> click file --> click Save: ctrl + s-->save as dialogue box
appears on the screen --> select Location: (D:)drive --> create and click New
folder -->rename folder(Java Programs) --> double click this Floder --> type
File Name: [Link] --> click save
How to compiler/Run in java file:
Open Window key+R --> type: cmd --> click Ok -->
C:\Users\LENOVO>cd\ (Enter key)
C:\>d: (Enter key)
D:\>cd java programs for nec (Enter key)
D:\Java Programs for NEC>cd AIML (Enter key)
D:\Java Programs for NEC\AIML>type [Link] (Enter key)
class Example1
{
public static void main(String[] args)
{
[Link]("Welcome to Java Programming");
}
}
D:\Java Programs for NEC\AIML>javac [Link] Enter
D:\Java Programs for NEC\AIML>java Example Enter
Welcome to Java Programming
Save this program: using .java and classname
Syntax:
[Link]
Ex: [Link]
Compiler the java program: using javac command
syntax: javac [Link]
Ex: javac [Link]
Run: Executed the program
Syntax: java classname
Ex: java Example
JAVA OUTPUT FUNCTION:
In Java, an output function means PRINTING OR DISPLAYING DATA to
the user.
The most common output functions are provided by the System class.
Function is:
1. [Link](): Prints the text without moving to a new line.
Example:
[Link]("Hello");
[Link](" World!");
Output:
Hello World!
2. [Link](): Prints the text and then moves the cursor to the next
line.
Example:
[Link]("Hello");
[Link]("World!");
Output:
Hello
World!
3. [Link](): Prints formatted output (like in C).
Formatting control string are:
int - %d
char - %c
string- %s
float - %f
double - %lf… etc.,
Example:
int age = 20;
String name = "Kalyan";
[Link]("My name is %s and I am %d years old.", name, age);
Output:
My name is Kalyan and I am 20 years old.
Notes:
print() → no new line
println() → with new line
printf() → formatted printing
Concatenating strings +:
[Link]("Hello " + "World");
Output:
Hello World
Here "Hello " and "World" are joined using +.
Example 2: Adding variables into output
int age = 20;
[Link]("My age is " + age);
Output:
My age is 20
Example 3: Difference between + (concatenation) and + (addition)
int a = 10, b = 20;
[Link]("Sum = " + (a + b));
Output:
Sum = 30
But if you forget parentheses:
[Link]("Sum = " + a + b);
Output:
Sum = 1020
⚠ Because it first makes "Sum = 10", then joins "20" as a string.
✅ Why we use + in output functions?
To join strings with variables or values.
It’s easier than writing multiple print statements.
Java automatically handles type conversion when concatenating.
The + symbol used inside output functions in Java is called: 👉 String
Concatenation Operator
Key Points:
In Java, + normally means addition (for numbers).
But when used with strings, it means concatenation (joining together).
Example
String name = "Kalyan";
int age = 20;
[Link]("Name: " + name + ", Age: " + age);
Output:
Name: Kalyan, Age: 20
Notes:
Here, + is the string concatenation operator, not arithmetic addition.
⚡ So in short:
+ with numbers → Addition Operator
+ with strings → String Concatenation Operator
Source Code:
class Outputfunctions
{
public static void main(String[] args)
{
//No new line
[Link]("Hello");
[Link]("ECE-F");
//with new line
[Link]("students");
[Link]("learn with java");
//using formatting controls
[Link]("Java Marks = %d",100);
String Concatenation Operator
[Link]("Hello " + "World");
}
}
Source code:
//Write a java program to demonstrative data types
class MyDatatypes
{
public static void main(String args[])
{
//Apply/remove Single line comment for notepad++ shortcut is: ctrl+Q
//Apply/remove Multiple line comment for notepad++ shortcut is:
ctrl+Shift + Q
int myNum = 5;
float f1 = 5.99f;
char myletter = 'D';
boolean > String myText = "Hello";
double d1=5.3;
byte b=122;
long l=10L;
short s=123;
[Link](" The Integer variab value is::"+myNum);
[Link](" The Float value is::"+f1);
[Link](" The Character value is::"+myletter);
[Link]("The Boolean variable value is::"+one);
[Link]("The String is::"+myText);
[Link]("The Double value is::"+d1);
[Link]("The Byte value is::"+b);
[Link]("The Long value is::"+l);
[Link]("The Short value is::"+s);
}
🔹 1. What is Input Function in Java?
Input function means any method that allows a program to take data from the user at
runtime.
Java does not have an input() function like Python, but it provides classes and methods
for input.
The most commonly used is the Scanner class.
🔹 2. Characteristics of Input Functions
Allow users to interact with the program.
Can take input of different data types (int, double, string, etc.).
Support formatted input (e.g., read numbers, words, lines).
Handle runtime data instead of fixed values.
Belong to [Link] or [Link] packages.
🔹 3. Types of Input in Java
1. Using Scanner Class (Beginner friendly)
2. Using BufferedReader + InputStreamReader
3. Using Console Class
4. Using Command-line Arguments
🔹 4. Syntax of Scanner Class
import [Link];
Scanner sc = new Scanner([Link]);
Here:
Scanner → class
sc → object (you can name it anything)
[Link] → standard input stream (keyboard)
🔹 5. Common Methods of Scanner Class
Method Description Example Input
nextInt() Reads an integer 25
nextDouble() Reads a decimal number 12.5
nextFloat() Reads a float 10.5f
next() Reads a single word (no spaces) Hello
nextLine() Reads a full line (with spaces) Hello Java
nextBoolean() Reads a boolean (true/false) true
nextLong() Reads a long integer 9876543210
nextShort() Reads a short integer 120
nextByte() Reads a byte value 127
🔹 6. Example Program
import [Link];
public class InputDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter integer: ");
int num = [Link]();
[Link]("Enter decimal: ");
double d = [Link]();
[Link]("Enter word: ");
String word = [Link]();
[Link](); // consume newline
[Link]("Enter sentence: ");
String sentence = [Link]();
[Link]("\n--- Output ---");
[Link]("Integer: " + num);
[Link]("Decimal: " + d);
[Link]("Word: " + word);
[Link]("Sentence: " + sentence);
[Link]();
}
}
public class ArithmeticExample1 {
public static void main(String[] args) {
// [Link]("Add ="+ (10+20));
// [Link]("Sub ="+ (10-20));
// [Link]("Mul ="+ (10*20));
// [Link]("Div ="+ (10/20));
// [Link]("MOd ="+ (10%20));
[Link]("Add ="+(10+20)+"\nSUB = "+(10-20));
public class ArithmeticExample2 {
public static void main(String[] args) {
int a = 10, b=20;
[Link]("Add ="+ (a+b));
[Link]("Sub ="+ (a-b));
[Link]("Mul ="+ (a*b));
[Link]("Div ="+ (a/b));
[Link]("MOd ="+ (a%b));
import [Link];
public class ArithmeticReadValues {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a, b;
[Link]("Enter A,B values?");
a = [Link]();
b = [Link]();
[Link]("Add = "+(a+b));
[Link]();
}
public class SwapingValuesUsingTemp {
public static void main(String[] args) {
int a=10, b=20, temp;
temp = a;
a = b;
b = temp;
/*
a = a+b;
b = a-b;
a = a-b */
[Link]("After Swap:\n A ="+a+"\n B = "+b);
}
Relational Operators: it return 0 or 1
class RelationalOperatorExample1
{
public static void main(String[] args)
{
// [Link]("Lessthan ="+(10<20));
// [Link]("Lessthan Equalsto ="+(100<=25));
// [Link]("Greaterthan ="+(5>20));
// [Link]("Greaterthan Equalsto ="+(10>=3));
// [Link]("Equalsto ="+(10==5));
// [Link]("NotEqualsto ="+(10!=5));
//or
//\n - New line \t - Horizontal Tab
[Link](" < :"+(10<20)+"\n <= :"+(10<=20)+"\n
> :"+(20>10)+"\n >= :"+(10>=20)+"\n == :"+(10==20)+"\n != :"+(10!=20));
int a=10,b=20;
[Link]("Lessthan ="+(a<b));
[Link]("Lessthan Equalsto ="+(a<=b));
[Link]("Greaterthan ="+(a>b));
[Link]("Greaterthan Equalsto ="+(a>=b));
[Link]("Equalsto ="+(a==b));
[Link]("NotEqualsto ="+(a!=b));
}
}
/*Logical Operators: To combine two or more relations we use logical
operator.
these are 3 types:
1. Logical AND(&&)
2. Logical OR(||)
3. Logical Not(!)
Truth Table:
Rel1 Rel2 Logical AND(&&) Logical OR(||) Logical Not(!)- rel(1)
1 1 1 1 0
1 0 0 1 0
0 1 0 1 1
0 0 0 0 1
Logical AND(&&): if all relation will be TRUE the Result will TRUE, if
any relation will be FALSE the Result will FALSE.
Logical OR(||): if any relation will be TRUE the Result will TRUE, if
all relation will be FALSE the Result will FALSE.
Logical NOT(!): if Relation will be TRUE the Result will be FALSE,
*/
class LogicalOperatorsExamples
{
public static void main(String[] args)
{
// [Link]("Logical AND(&&) ="+ (10<=20 && 10>20));
// [Link]("Logical OR(||) ="+ (10>20 || 10<20));
// [Link]("Logical NOT(!) ="+ (!(10<20));
int a = 10;
int b = 20;
[Link]("logical AND = "+(a<b && b>a));
[Link]("logical OR = "+(a>b || b>a));
[Link]("logical NOT = "+(!(a<=b)));
}
}
Assignment Operators in Java: Assignment operators are used to assign
values to variables. the assign value from right to left.
the right operand may be Constant, variable and Expression.
syntax:
variable = value;
//But Java also provides shorthand operators for common operations.
Short hand Assignment Table:
| Operator | Meaning | Example | Result |
--------------------------------------------------------------------
|= | Assign |a=5 |a=5 |
| += | Add and assign | a += 3 | a = a + 3 |
| -= | Subtract and assign | a -= 2 | a = a - 2 |
| \*= | Multiply and assign | a \*= 4 | a = a \* 4 |
| /= | Divide and assign | a /= 5 | a = a / 5 |
| %= | Modulus and assign | a %= 2 | a = a % 2 |
| &= | Bitwise AND assign | a &= b | a = a & b |
| \|= | Bitwise OR assign | a \|= b | a = a \| b |
| ^= | Bitwise XOR assign | a ^= b | a = a ^ b |
| <<= | Left shift assign | a <<= 2 | a = a << 2 |
| >>= | Right shift assign | a >>= 2 | a = a >> 2 |
| >>>= | Unsigned right shift assign | a >>>= 2 | a = a >>> 2 |
1. Simple Assignment (`=`): Assigns the right-hand value to the left-hand
variable.
int a = 10; // assigns 10 to variable a
2. Add and Assign (`+=`):
Adds right-hand value to the variable and assigns the result.
int a = 5;
a += 3; // same as a = a + 3 → result: a = 8
3. Subtract and Assign (`-=`): Subtracts right-hand value from the variable and
assigns the result.
int b = 10;
b -= 4; // same as b = b - 4 → result: b = 6
4. Multiply and Assign (`*=`): Multiplies variable by right-hand value and
assigns the result.
int c = 7;
c *= 2; // same as c = c * 2 → result: c = 14
5. Divide and Assign (`/=`): Divides variable by right-hand value and assigns
the result.
int d = 20;
d /= 5; // same as d = d / 5 → result: d = 4
6. Modulus and Assign (`%=`): Takes modulus (remainder) and assigns the
result.
int e = 15;
e %= 4; // same as e = e % 4 → result: e = 3
7. Bitwise AND and Assign (`&=`): Performs bitwise AND and assigns the
result.
int x = 5; // (0101 in binary)
x &= 3; // (0011 in binary) → result: 0001 (decimal 1)
8. Bitwise OR and Assign (`|=`): Performs bitwise OR and assigns the result.
int y = 5; // (0101 in binary)
y |= 3; // (0011 in binary) → result: 0111 (decimal 7)
9. Bitwise XOR and Assign (`^=`): Performs bitwise XOR and assigns the result.
int z = 5; // (0101 in binary)
z ^= 3; // (0011 in binary) → result: 0110 (decimal 6)
10. Left Shift and Assign (`<<=`)
Shifts bits to the left and assigns.
int p = 3; // (0011 in binary)
p <<= 2; // shift left 2 → (1100 binary) = 12
11. Right Shift and Assign (`>>=`)
Shifts bits to the right and assigns.
int q = 16; // (10000 in binary)
q >>= 2; // shift right 2 → (0100 binary) = 4
12. Unsigned Right Shift and Assign (`>>>=`)
Shifts bits to the right, filling with zeros (for negative numbers also).
int r = -16;
r >>>= 2; // shifts with zero-fill (result depends on binary representation)