Java is a statically-typed, object-oriented programming language with a clear and concise syntax. Understanding Java's syntax, data types, and variables is essential for writing effective and efficient programs. This tutorial will introduce you to the basics of Java syntax, the different data types available, and how to declare and use variables.
Java Syntax, Data Types, and Variables Tutorial
By the end of this tutorial, you'll have a solid understanding of how to write basic Java programs, declare variables, and use different data types to store and manipulate data.
Java Syntax Basics
- Case Sensitivity: Java is case-sensitive, meaning
myVariable
andmyvariable
are treated as different identifiers. - Class Names: Class names should start with an uppercase letter (e.g.,
MyClass
). - Method Names: Method names should start with a lowercase letter (e.g.,
myMethod()
). - Main Method: Every Java program must have a
main
method, which is the entry point of the program:public static void main(String[] args) { // Code to execute }
- Semicolons: Each statement in Java must end with a semicolon (
;
). - Blocks: Code blocks are enclosed in curly braces
{}
.
Java Data Types
Java supports two categories of data types: primitive and reference.
- Primitive Data Types:
byte
: 8-bit integer (range: -128 to 127).short
: 16-bit integer (range: -32,768 to 32,767).int
: 32-bit integer (range: -231 to 231-1).long
: 64-bit integer (range: -263 to 263-1).float
: 32-bit floating-point number.double
: 64-bit floating-point number.char
: 16-bit Unicode character (e.g.,'A'
).boolean
: Representstrue
orfalse
.
- Reference Data Types:
- Objects: Instances of classes (e.g.,
String
,ArrayList
). - Arrays: Collections of elements of the same type (e.g.,
int[] numbers = {1, 2, 3};
).
- Objects: Instances of classes (e.g.,
Java Variables
- Variable Declaration:
- Syntax:
dataType variableName;
(e.g.,int age;
). - You can also initialize a variable during declaration:
int age = 25;
.
- Syntax:
- Variable Naming Rules:
- Variable names must start with a letter, underscore (
_
), or dollar sign ($
). - Names cannot contain spaces or special characters (except
_
and$
). - Names cannot be Java keywords (e.g.,
int
,class
).
- Variable names must start with a letter, underscore (
- Variable Scope:
- Local Variables: Declared inside a method or block. They are only accessible within that block.
- Instance Variables: Declared inside a class but outside any method. They belong to an instance of the class.
- Static Variables: Declared with the
static
keyword. They belong to the class and are shared across all instances.
Example: Using Variables and Data Types
public class Main {
public static void main(String[] args) {
// Primitive data types
int age = 25;
double salary = 50000.75;
char grade = 'A';
boolean isJavaFun = true;
// Reference data types
String name = "John Doe";
int[] numbers = {1, 2, 3, 4, 5};
// Output variables
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Numbers: " + Arrays.toString(numbers));
}
}
This example demonstrates how to declare and use variables of different data types in Java. You can modify and experiment with the code to deepen your understanding.