Java Syntax and Basics
1. Introduction
Java is a versatile, high-level programming language that follows the object-oriented programming paradigm. It is widely used for building enterprise-scale applications, web applications, and mobile apps.
2. Data Types
Java supports several primitive data types:
- int: Integer type, e.g.,
int age = 30;
- double: For decimal numbers, e.g.,
double salary = 2500.50;
- char: For characters, e.g.,
char grade = 'A';
- boolean: For true/false values, e.g.,
boolean isJavaFun = true;
3. Variables
Variables are containers that hold data values. In Java, variables must be declared with a specific data type.
int number = 10;
String name = "John";
4. Operators
Java provides various operators for performing operations on variables:
- Arithmetic Operators:
+, -, *, /, %
- Relational Operators:
==, !=, >, <, >=, <=
- Logical Operators:
&& (AND), || (OR), ! (NOT)
5. Control Structures
Control structures determine the flow of execution in a program. Common control structures include:
5.1 Conditional Statements
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
5.2 Loops
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
6. Methods
Methods are blocks of code that perform a specific task. They can take parameters and return values.
public int add(int a, int b) {
return a + b;
}
FAQ
What is Java?
Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible.
What are the main features of Java?
Main features include platform independence, object-oriented, automatic memory management, and strong typing.
Is Java a compiled language?
Yes, Java is compiled to bytecode, which can be executed on any Java Virtual Machine (JVM).