ASSIGNMENT (Java)
ASSIGNMENT (Java)
1. Java versions :
• Java released different versions , with new features,improvements and less errors.
• It includes JDK 1.0 TO JDK 23(Latest version).
• High Level Language: This type of language is closer to human language because
they are easy to read , understand and write. Machine or Computer can’t understand
this language only humans can so, it is called High Level Programming Langauge.
Examples: python, c, c++ , java etc…
3. Data Types:
1. Primitive Data types:
TYPE SIZE EXAMPLE
byte 8-bit byte a = 10;
short 16-bit Short s = 1000;
int 32-bit int b = 50;
long 64-bit long num = 150000000L;
float 32-bit float decnum = 5.4f;
double 64-bit double dec = 19.99;
char 16-bit char c = ‘B’;
boolean 1-bit boolean isJavaFun = true;
4. Variables:
• Variables are like a containers which stores some value according to the specified
datatype.
• We assign value to the variable by using assignment operator “=”.
• One variable can only store one value.
• Example : int n = 10;
• Here, int is datatype , n is variable and 10 is value.
5. Naming Conventions:
Naming conventions are a set of rules for naming variables, methods, classes, etc., to
make code more readable. For example:
6. Operators
• Operators are symbols that perform operations on variables and values.
1. Arthematic operators:
OPERATOR NAME EXAMPLE
+ ADDITION A+B
- SUBTRACTION A-B
* MULTIPLICATION A*B
/ DIVISION A/B
% MODULUS A%B
++ INCREMENT A++
-- DECREMENT A--
2.Assignment Operators:
OPERATOR EXAMPLE
= A=3
+= A=A+3
-= B=B-4
*= X=X*3
/= Y=Y/3
%= X=X%3
&= Y=Y&6
|= Z=Z|5
^= X=X^2
>>= K = K>>4
<<= P = P<<2
3.Comparision Operators
OPERATOR NAME EXAMPLE
== EQUAL TO A= = B
4.Logical operators:
OPERATOR NAME USE EXAMPLE
}
package Condition;
import java.util.Scanner;
public class Evenodd {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
if(a<5) {
System.out.println(a);
}
}
}
2.if else condition:
if (condition) {
// Code to execute if the condition is true
} else
{ // Code to execute if the condition is false
}
package Condition;
import java.util.Scanner;
public class Evenodd {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
if(a%2==0) {
System.out.println("Even number");
}
else {
System.out.println("Odd number");
}
}
}
3. else if ladder:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the above conditions are true
}
Example:
package Condition;
import java.util.Scanner;
8.Continue Vs Break:
• Continue: Skips the current iteration of a loop and moves to the next one.
• Break: Exits the loop entirely.
Example:
package Loops;
10. Method:
A method is a block of code that performs a specific task. In Java, methods are defined inside
a class.
package test;
11. String:
A String is a data type that represents a sequence of characters, like words or sentences.
package Assignment;
12. Scanner:
import java.util.Scanner;
13. Object:
An object is an instance of a class. If Car is a class then myCar is object.
Example:
Car myCar = new Car(); // 'myCar' is an object of class 'Car'.
}
}
15. Integer.parseInt():
Example:
16.Access Modifiers:
The main method is the entry point of a Java application. It is like heart of java.
Syntax:
public static void main(String[] args) {
// Code to run
18. Import:
import is used to bring other classes or packages into a Java program.
Example:
import java.util.Scanner; // Imports the Scanner class for input.
19. Package:
A package is like a folder that organizes classes and interfaces.
Example:
package mypackage; // Declares this class is part of 'mypackage'.
Comments are used to explain code and are ignored by the compiler.
• Single-line Comment: // This is a comment
• Multi-line Comment: /* This is a multi-line comment */