Operators
Java divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Example - +,-,%
Assignment Operators
Assignment operators are used to assign values to variables.
Example - =,+=,-=
Comparison Operators
Comparison operators are used to compare two values (or variables).
Example - ==,<,>
Logical Operators
Logical operators are used to determine the logic between variables or values:
Example - &&,!<
Array
An array in programming is like a collection of similar things grouped under a
common name. Arrays can be of various types and sizes. To access a specific item in
an array, you use its position, called an index. Arrays are useful for organizing related
data. In Java, arrays work a bit differently than in C/C++.
one-dimensional
A one-dimensional array is essentially a list of items that are all of the same type. To
create one, you first need to declare it by specifying the type of items it will hold and
give it a name. Here's a short
example: int[ ] monthDays;
Java uses a combination of compilation and interpretation to execute code, and the
process involves several steps:
Java Code Execution Steps:
Compilation: Java source code (in .java files) is translated into bytecode using the
Java compiler (javac).
Bytecode: Bytecode is stored in .class files, which are platform-independent.
Interpretation: When running a Java program, the Java Virtual Machine (JVM)
interprets bytecode and translates it into machine code specific to the host system (JIT
compilation).
JDK (Java Development Kit):
JDK is a package by Oracle for Java development.
It includes essential tools like the Java compiler (javac) for code compilation.
JDK also contains the Java Runtime Environment (JRE), which is needed to run Java
applications on your computer.
Java's motto: "Write once, run anywhere" means code can run on any platform with a
compatible JVM.
Features of Java
Simple
Secure
Portable
Object-oriented
Robust
Multithreaded
Architecture-neutral
Interpreted
High performance
Distributed
Dynamic
Java Data Types are used to classify and store different types of data in Java
programs. Here are the basic data types in Java:
Primitive Data Types: A primitive data type specifies the size and type of variable
values, and it has no additional methods.
int: Stores whole numbers (e.g., 42).
double: Stores decimal numbers (e.g., 3.14).
char: Stores a single character (e.g., 'A').
boolean: Stores true or false values.
byte: Stores small integers (-128 to 127).
short: Stores small integers (-32,768 to 32,767).
long: Stores large whole numbers (e.g., 1234567890L).
float: Stores decimal numbers with less precision (e.g., 2.0f).
Reference Data Types:
Objects: Used for creating user-defined data types.
Arrays: Collections of elements of the same type.
Derived Data Types:
String: Represents a sequence of characters (e.g., "Hello, Java!").
Array: A collection of elements, indexed by number.
Java Control Statements
Control statements in Java determine the flow of a program. They fall into these categories:
1. Selection Statements:
if: Makes decisions based on a condition. If the condition is true, a block of code executes.
switch: Used for multiple conditional branches. It selects a code block to execute based on a
specific value.
2. Iteration Statements (Loops):
for: Repeats a block of code a specific number of times.
while: Repeats a block of code while a condition is true.
do-while: Repeats a block of code at least once and continues while a condition is true.
3. Jump Statements:
break: Exits a loop or switch statement prematurely.
continue: Skips the current iteration of a loop and continues with the next iteration.
return: Exits a method and can return a value