Java Basics: Hello World, Variables, Arrays, Expressions, Control Structures, and Object‑Oriented Concepts
This tutorial introduces Java fundamentals for beginners, covering the Hello World program, class and method syntax, variable declaration, arrays, expressions, control flow statements, and basic object‑oriented concepts with clear explanations and runnable code examples.
Hello World
The first example shows a simple HelloWorld.java program that prints the string "Hello World!" on the console, illustrating the basic structure of a Java class, the main method, and a statement.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}Variables
Java is a statically‑typed language; variables must be declared with a type before use. The tutorial demonstrates declaration, assignment, and printing of an integer variable.
public class Test {
public static void main(String[] args) {
System.out.println("Declare in the middle:");
int a;
a = 5;
System.out.println(a); // print an integer
}
}It also notes that a variable can be declared and initialized in a single statement, e.g., int a = 5;, and that comments are introduced with //.
Arrays
Arrays store multiple values of the same type. Declaration syntax ( int[] a;) and allocation with new are shown, as well as inline initialization.
int[] a; // declaration
int[] a = new int[100]; // allocate 100 ints
int[] a = new int[] {1, 3, 5, 7, 9}; // allocate and initializeExpressions
Expressions combine variables, constants, and operators to produce a value. Examples include arithmetic ( 1 + 1), relational ( a > 4.2), boolean ( true && false), and bitwise operators ( &, |, ^, ~, <<, >>).
Control Structures
The tutorial covers the standard C‑style control flow constructs in Java: if‑else, while, do…while, for, break, continue, and switch. Sample syntax is provided for each.
if (condition1) {
// statements
} else if (condition2) {
// statements
} else {
// statements
}
while (condition) {
// statements
}
do {
// statements
} while (condition);
for (initial; condition; update) {
// statements
}
switch (expression) {
case 1:
// statements
break;
default:
// statements
break;
}Object‑Oriented Basics
Objects are abstractions with identity, state (data members/fields), and behavior (methods). Classes define the blueprint; instances are created with new. The tutorial defines a simple Human class with a breath() method and an int height field, then shows how to instantiate and use it.
class Human {
void breath() {
System.out.println("hu...hu...");
}
int height;
}
public class Test {
public static void main(String[] args) {
Human aPerson = new Human();
aPerson.breath();
System.out.println(aPerson.height);
}
}Summary
Java’s syntax resembles C/C++ but has its own nuances; careful attention to details such as type declarations, array allocation, and object identity is required. The language is fully object‑oriented, and mastering these fundamentals provides a solid foundation for further Java development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
