JAVA ASSSIGNMENT:
1. Some basics Java is a high-level, object-oriented programming language developed by Sun
Microsystems and later owned by Oracle. It is similar to C++ but easier and safer
because it does not use pointers. Java supports secure and bug-free coding and has
a large standard library to help developers build applications easily.
Java programs are compiled into bytecode, which runs on the Java Virtual Machine
(JVM). This makes Java platform-independent, meaning it can run on any system with
Java installed.
Java has different editions: Java SE for desktop apps, Java ME for small devices,
Java EE for enterprise applications, and JavaFX for modern GUI applications.
2. Overall Program Structure Java follow this basic rule:
1. File must saved with the name “[Link]”.
2. Main method() = program entry
3. Code insert always inside the curly brackets {}
[Link] example:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
3. How to comment/uncomment Coming to stew insert the notes but it does not change anything in the code.
program lines That's how comment we do:
[Link] line comments
Start from //
2. Multi lines comment
Start from /* and ends by …*/
3. JavaDoc isn't special documents comment to utilize the some important programs
Example:
/**
* This program prints Hello World
* Author: Student
*/
public class Hello {
public static void main(String[] args) {
[Link]("Hello World");
}
}
4. Supported Data Types Data data type used to tell Java what kind of data available can store example
numbers character true and false etc
Data types: Size: Description:
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
byte 1 byte Stores very small whole numbers
short 2 byte Stores small whole number
int 4 byte Stores whole number
long 8 byte Stores large amount of whole number
float 4 byte
double 8 byte stores large decimal
char 2 byte Written in single quote
boolean Stores true or false and used for condition
5. How to Define a Variable Variable is the container that holds a value in program during execution.
For example:
int a=18;
int b=2;
int sum=a+b;
6. Assigning values to variables Assigning a value to a variable means giving a variable some data to store so that we can use it
later in the program. A variable is like a container in memory, and assigning puts something into
that container.
7. ---and --- typecasting Type casting is the process of converting one data type into another. Sometimes, Java does it
automatically, and sometimes we need to do it manually. This is useful when we want to combine
different types of data or store values in a different type.
Two types of casting
[Link] (Automatic / Implicit)
[Link] (Manual / Explicit)
8. Mathematical Expression Operators: Meaning: Precedence:
Writing/Evaluation Rules + Addition Lowest
▪ Available mathematical - Subtraction Lowest
operators / Division Highest
▪ operators precedence * Multiplication Highest
▪ order of evaluation of %. Reminder Highest
expression += Add and assign It add a values in variable and store the result back
in same variable
-= Sub and assign It substract a value in vaiable and stores result in
same value
*= Multiply and It multiply a value in varible and remain the result
assign
/= Division and It divide the real value an stores result back
assign
%=. Modulus and It find the reminder after divison and stores the value
assign back in variable
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
++ Increment Increse the value
- - Decrement Decrease the value
9. relational expressions < less than
> greter than
<= less than or equal
>= greater than or equal
== equal to
!= Not equal
Higher order evulated first
logical Expression logical Logical AND operator !
Writing/Evaluation Rules and their Logical NOT operator &&
combination Logical OR operator ||
• General Rules
Logical operators work only with boolean values (true or false)
The result is always boolean
Mostly used in conditions (if, while, for)
In AND both conditions must be true
If first condition is false, Java does not check the second one (short-circuit
rule)
At least one condition must be true
If first condition is true, Java skips the second condition
Works on only one condition
Reverses the result
10. Bit operators Bitwise AND &
Bitwise OR ‘
Bitwise XOR ^
Bitwise NOT ~
Bitwise left shift <<
Bitwise right shift >>
Unsigned Right Shift >>>
What does >>> do?
It shifts bits to the right
It fills empty left bits with 0
It works even for negative numbers
11. Overall precedence of Highest precedence:
mathematical, relational and 1 ++ ! ~
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
logical operators Mathematical expression:
2 * / %
3+−
Expression always solve from rigth to left
Relational operator:
4 << >> >>>
5 < <= > >=
Example
int a = 8;
int b = 5;
boolean result = a > b;
[Link](result);
Equality operator:
6 == !=
Example:
int x = 5;
int y = 10;
[Link](x < y); // true
[Link](x == y); // false
Bitwise operator:
7 &
8 ^
9 |
Example:
int a = 6; // 110
int b = 3; // 011
int result = a & b;
[Link](result);
Logical operator:
10 &&
11 ||
12 = op=
Example:
int a = 10;
int b = 5;
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
boolean result = (a > b) && (b > 0);
[Link](result);
Rules:
Java always evaluates in this order:
Arithmetic → Relational → Logical
Always higher order solve first
We use parentheses () to control the order
All orders Example:
int a = 5, b = 3, c = 2;
boolean result = !(a + b * c > 10) || (a > b && b < c) ^ (a & b > 0);
12. String handling String use to store a text,such as word sentence or name.
▪ Define String is class , not the primitive data type,and their values remain unchanged.
▪ Initialize Example:
String message = "Hello Java";
[Link](message);
Output= Hello Java
13. Statement separators ; this is an semicolon, without semicolon code won't run because program don't
know where statement end.
Without semicolon we always get error in code
Semicolon don't use in class definition and method definition.
14. Input Statements This input statement use to take data(input) from user, which we use in code as
scanner. This allow the user to write data from keyboard. Their common statement:
import [Link];
It has data type stream method include boolean,byte,int, float etc ,we will use as
per need.
One simple example:
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
[Link](a);
}
}
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
15. Output Statements An output statement in Java is a statement used to display information on the
screen, such as [Link]().
Simple example:
[Link]("Hello Java");
16. Available Library import [Link];
Functions
[Link]()
.tan()
.log()
.log10()
.sqrt()
.pow(base, radix)
.exp()
.asin()
.acos()
.atan()
.cosh()
.sinh()
.tanh()
[Link]
Rules for all mathematical expression:
Math class methods are static.
Methods are accessed using [Link]().
All angle values are in radians, not degrees.
[Link](a, b) takes two arguments.
[Link] is a constant value.
Return type of Math methods is double.
17. Selection Statements if,if else,else if,switch are some selective statement
Rules of Selective Statements
Selective statements are used for decision making.
A condition must be written inside parentheses ( ).
Conditions return true or false.
Code blocks are written inside curly braces { }.
Only the block with true condition is executed.
switch works with fixed values, not conditions.
18. Iterative Statements for, while,do-while ate iterative statements
An iterative statement is a statement that executes a set of instructions
repeatedly based on a condition.
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
Example:
for (int i = 1; i <= 5; i++) {
[Link](i);
}
19. Arrays
▪ Declare An array is a collection of elements of the same data type stored in contiguous
▪ Create memory locations.
▪ Initialize Rules of Array
▪ use Array index starts from 0.
All elements must be of same data type.
Array size is fixed.
Array elements are accessed using index number.
•Single Dimensional Array
A single dimensional array stores elements in one row using one index.
int[] a = {10, 20, 30};
[Link](a[0]);
•Multi-dimensional array
A multi dimensional array stores elements in rows and columns using two indexes.
int[][] a = {
{1, 2},
{3, 4}
};
[Link](a[0][1]);
▪
▪
20. Modularization/Functions Modularization in Java also known as function or [Link] is the process of dividing a program into
small functions or methods to make it easy to understand and manage.
Rules of Modularisation methods in Java:
A method must be written inside a class.
Every method has a name.
A method may or may not return a value.
main() is the starting method of a program.
Methods are called using their method name.
Code reuse is possible using methods.
21. Passing Values from /to Passing value to a function means sending data to a method using parameters, and
Modules and Functions passing value from a function means returning a value using the return statement.
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5
JAVA ASSSIGNMENT:
22. How arrays are passed to Array is passed to a method by reference.
and from modules/functions The array name is passed as an argument.
The method parameter must be an array type.
Changes made inside the method are reflected outside.
Arrays can be passed to and from methods.
23. External File/Data External file data handling means reading data from a file and writing data to a
Handling file that is stored outside the program.
24. Support for User Defined User defined data type means a data type made by the programmer.
Data types When built in data types (like int, float) are not enough, we can create our own
data type.
25. Integration with Operating Integration with operating system means Java can interact and work with the
System operating system to use system resources because of this, Java programs can run
easily on Windows, Linux, and macOS.
26. Recursion Recursion in Java means a method calls itself to do a task. It solves a big
problem by breaking it into smaller same problems. A base condition is needed to
stop the method, otherwise it will run again and again. Recursion makes code
easier, but too much use can cause errors.
27. Graphics Graphics in Java is used to draw shapes, text, and images on the screen using Java
code. Fonts are available in our operating systems.
28. Exception handling Exception handling in Java means catching and managing errors that happen while a
program is running so the program doesn’t crash.
29. OOP OOP stands for object oriented program which is a way of writing a programs by
object instead of just functions and dat.
30. Using existing class This means we don't need to rewrite the code from scratch, we can begin the new
class that already exist to finsh another work.
Page PAGE \* MERGEFORMAT 1
of NUMPAGES \*
MERGEFORMAT 5