Home | Computer Science

Java Key Concepts


Introduction

We'll cover the `final` keyword, how to change data types (type casting), the `+` operator for combining values, and declaring multiple variables.


Java Key Concepts

Final Keyword:
- The `final` keyword is used to define constants. Once a value is assigned to a `final` variable, it cannot be changed.
Changing Data Types (Typecasting):
- You can change a data type by casting it. For example, converting a `double` to an `int` removes the decimal part.
Using the + Operator:
- The `+` operator serves two purposes in Java: it adds numbers and concatenates (joins) strings.
Declaring Multiple Variables:
- You can declare multiple variables of the same type in a single line, separated by commas.

Program Example:
Code:

public class JavaKeyConceptsExample {
public static void main(String[] args) {

// Final keyword
final int MAX_HEIGHT = 100;
System.out.println("Final variable (MAX_HEIGHT): " + MAX_HEIGHT);

// Typecasting
double myDouble = 9.78;
int myInt = (int) myDouble; // Casting double to int
System.out.println("Typecasted double to int: " + myInt);

// + operator for addition and concatenation
int a = 5;
int b = 10;
System.out.println("Sum of a and b: " + (a + b));
String firstName = "John";
String lastName = "Doe";
System.out.println("Full Name: " + firstName + " " + lastName);

// Multiple variable declaration
int x = 100, y = 200, z = 300;
System.out.println("Multiple Variables: " + x + ", " + y + ", " + z);
}
}

OUTPUT