Professional Documents
Culture Documents
In Java, keywords are reserved words that have predefined meanings and
cannot be used as identifiers (such as variable names, method names, or class
names). Here's a list of Java keywords:
```java
Public class ConstantsExample {
// Integer constant
Public static final int MAX_VALUE = 100;
// String constant
Public static final String GREETING = “Hello, world!”;
In this example:
- `MAX_VALUE` is an integer constant with a value of 100.
- `GREETING` is a string constant with the value “Hello, world!”.
Constants are typically declared as `public` and `static` if they are intended to be
accessed from other classes without the need to create an instance of the class. The
`final` keyword ensures that the value of the constant cannot be modified once
assigned.
In Java, variables are containers used to store data values. There are different types
of variables and data types:
### Variables:
1. **Local Variables**: Declared inside a method, constructor, or block. They
are only accessible within the scope in which they are declared.
```java
Public void exampleMethod() {
Int localVar = 10;
}
```
```java
Public class MyClass {
Int instanceVar = 20;
}
```
3. **Class Variables (Static Variables)**: Declared with the `static` keyword
within a class but outside any method or constructor. They are shared among
all instances of the class.
```java
Public class MyClass {
Static int classVar = 30;
}
```
```java
Byte b = 10;
Int I = 100;
Double d = 10.5;
Char c = ‘A’;
Boolean flag = true;
```
```java
String str = “Hello”;
Int[] arr = {1, 2, 3};
MyClass obj = new MyClass();
```
In Java, reference data types are used to store references (memory addresses) to
objects rather than the actual data itself. Unlike primitive data types, which store
the actual values, reference data types store the location of where the data is stored
in memory.
3. **Arrays**: Arrays are a special type of object used to store multiple values
of the same type in a contiguous memory location. They can be single-
dimensional, multi-dimensional, or jagged arrays.
```java
// Declaring and initializing a single-dimensional array
Int[] numbers = {1, 2, 3, 4, 5};
```java
// Creating an ArrayList of Strings
ArrayList<String> list = new ArrayList<>();
List.add(“apple”);
List.add(“banana”);
List.add(“orange”);
```
```java
// Reference to an object that implements the Runnable interface
Runnable runnable = () -> System.out.println(“Hello from Runnable!”);
```
These reference data types allow Java programmers to work with complex data
structures and facilitate object-oriented programming principles such as
encapsulation, inheritance, and polymorphism.
Understanding variables and data types is fundamental in Java programming, as
they form the building blocks for creating and manipulating data within your
programs.
Sure, here’s an overview of decision making, branching and looping, labeled loops,
and jump statements in Java:
These operators allow you to perform a wide range of operations in Java, from
simple arithmetic to complex logical and bitwise operations.
```java
If (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
```
```java
Switch (expression) {
Case value1:
// code to execute if expression equals value1
Break;
Case value2:
// code to execute if expression equals value2
Break;
Default:
// code to execute if expression does not match any case
}
```
```java
Variable = (condition) ? value1 : value2;
```
### 2. Branching and Looping:
Branching and looping statements control the flow of execution in a program.
```java
While (condition) {
// code to execute while condition is true
}
```
```java
Do {
// code to execute at least once
} while (condition);
```
```java
For (initialization; condition; iteration) {
// code to execute while condition is true
}
```
```java
labelName: while (condition) {
// code
Break labelName; // breaks out of the labeled loop
Continue labelName; // continues with the next iteration of the labeled loop
}
```
These constructs are essential for controlling the flow of execution in Java
programs, allowing for efficient decision making, branching, looping, and control
flow.
```java
Public class MyClass {
// Class members (variables and methods) go here
}
```
// Constructor
Public MyClass(int num, String name) {
This.num = num;
This.name = name;
}
// Method
Public void display() {
System.out.println(“Number: “ + num + “, Name: “ + name);
}
}
```
```java
Public class Main {
Public static void main(String[] args) {
// Creating an object of MyClass
MyClass obj = new MyClass(10, “John”);
```java
Public class MyClass {
Private int num;
Private String name;
// Default constructor
Public MyClass() {
// Initialization code
}
// Parameterized constructor
Public MyClass(int num, String name) {
This.num = num;
This.name = name;
}
}
```
```java
// Superclass
Public class Animal {
Public void sound() {
System.out.println(“Animal makes a sound”);
}
}