0% found this document useful (0 votes)
8 views10 pages

Java Basics Structure and Syntax

Uploaded by

sirjcalanog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Java Basics Structure and Syntax

Uploaded by

sirjcalanog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Basics:

Structure and
Syntax
presentation by:
sirjmastermind
The Blueprint: Understanding Java
Program Structure
Just like building a house requires a blueprint, writing a Java program follows a
specific structure. Knowing these fundamental components is crucial for creating
functional and organized code.

Package Declaration Import Statements Class Declaration


Declaration The core of every Java
(Optional) Allows your program to program. Everything in
Groups related classes use classes from other Java exists within a class.
together. Think of it as a packages, like borrowing It's the blueprint for
folder for your Java files. tools from a toolkit. creating objects.

Main Method Statements Key Concept: Java is a


and Expressions class-based language,
The starting point of your The instructions and meaning all executable code
program's execution. It's actions your program resides within classes.
where the program "wakes performs, like sentences in No class, no program!
up" and begins to run. a story.
Bringing Code to Life:
Compiling and Running Java
01 Write Your Code
Write your Java source code in a text editor and save it with a .java extension
(e.g., HelloWorld.java).
02 Compile with javac
Use the Java Compiler (javac) to translate your .java file into bytecode (.class file).
This bytecode is platform-independent.
03 R un with java
Execute the bytecode using the Java Virtual Machine (java command).
The JVM interprets the bytecode and runs your program.
This process ensures that Java applications can
run on any `device that has a Java Virtual Machine
Example: (JVM) installed, regardless of the underlying
javac HelloWorld.javajava HelloWorld operating system. This is often referred to as
"Write Once, Run Anywhere."
The Foundation: Class Declaration
Every Java program revolves around classes. A class is a template or blueprint
from which objects are created. It defines the structure and behavior that its
objects will have.
public class HelloWorld { // class body}

public class HelloWorld


This is an access This is a Java keyword This is the name of the
modifier. It means the that is used to declare a class. By convention,
class can be accessed class. class names start with
from any other class. an uppercase letter and
use "PascalCase" (e.g.,
MyNewClass). It must
match the filename.

Important Rule: Each Java program must contain at least one class, and the
class containing the main method (your program's entry point) must have a
name that exactly matches the filename.
The Starting Line: The Main Method
The main method is the heart of every standalone Java application.
When you run a Java program, the Java Virtual Machine (JVM) looks for this
specific method to begin execution. It's your program's entry point.
public static void main(String[] args) { // code to execute}
Breaking Down the Main Method:
• public: This keyword makes the main method accessible to the
Java Virtual Machine (JVM) from anywhere.
• static: This means the main method belongs to the class itself, not to a specific object
of the class. You don't need to create an object of the class to call this method.
• void: This indicates that the main method does not return any value after its execution.
• String[] args: This is a parameter for command-line arguments. It's an array of
String objects that can receive data passed when the program is run from the
command line.
Building Blocks of Information:
Java Data Types
In Java, every variable must have a defined data type.
This tells the compiler what kind of data the variable will hold and
how much memory to allocate for it.
Java is a strongly typed language, which means data types are strictly enforced.
Primitive Data Types
These are the most basic data types, representing single values.
They are stored directly in memory.
• Integer Types: byte, short, int, long (for whole numbers)
• Floating-Point Types: float, double (for numbers with decimal points)
• Character Type: char (for single characters)
• Boolean Type: boolean (for true/false values)
Reference Data Types
These types do not store the actual values directly but rather references
(memory addresses) to the objects that contain the values.
• Classes: (e.g., String, custom classes you create)
• Interfaces: (a blueprint of a class)
• Arrays: (collections of elements of the same type)
Storing Information: Variables in Java
Variables are like containers for storing data values. Before you can use a variable, you must declare it,
specifying its data type and giving it a name. You can also assign an initial value when declaring it.
Declaration Syntax:
int age = 18;String name = "Lemery";

Notice how the type (int, String) comes before the variable name.
Types of Variables:
Local Variables
Declared inside a method, constructor, or block. They are created when the method is entered and
1 destroyed when the method exits. Only accessible within that block.
Instance Variables (Non-Static Fields)
Declared inside a class but outside any method. They belong to an object and are created when an
2 object is created using the new keyword.
Static Variables (Class Variables)
3 Declared with the static keyword inside a class but outside any method. They belong to the class
itself and are shared among all objects of that class.
Explaining Your Code: Comments in Java
Comments are non-executable lines of code that programmers use to explain what the code does. They are
ignored by the compiler and are essential for making your code readable and understandable, both for yourself in
the future and for other developers.

Single-Line Comments Multi-Line Comments Documentation (Javadoc)


Start with two forward slashes Start with /* and end with */.
Comments
(//). Everything from // to the Anything between these symbols Start with /** and end with */.
end of the line is a comment. is a comment, spanning multiple Used to generate API
lines. documentation automatically
// This is a single-line /*This is a multi-line using the javadoc tool.
commentint x = 10; // comment.It can span
Initialize x across several lines.*/ /*** This method
calculates the sum of
two numbers.* @param a
The first number.*
@param b The second
number.* @return The sum
of a and b.*/
Putting It All Together: A Simple Java Program
Let's look at a classic "Hello, World!" program. This simple example demonstrates the basic structure,
including a class, the main method, a comment, and an output statement.`
public class HelloWorld { public static void main(String[] args) {
// Print greeting to the console System.out.println("Hello, world!");
}}

This program perfectly illustrates:


• The mandatory class declaration (public class HelloWorld).
• The entry point of the program (public static void main).
• A single-line comment for clarity.
• An output statement (System.out.println()) to display text on the console.
Recap and Next Steps
Today, we've covered the fundamental building blocks of Java programming. Mastering these basics is essential
for your journey into more complex Java concepts.

Class-Based & Strongly Typed Main Method: The Program's Start


Remember that every piece of Java code lives inside The public static void main(String[] args)
a class, and variables must have their data types method is where your program begins execution.
explicitly declared.
Compile, Then Run Precision is Key
Java code needs to be compiled into bytecode Exact syntax is critical for successful compilation
(.class) by javac before being executed by the JVM and execution in Java.
using the java command.

Questions & Discussion


What aspects of Java's structure or syntax are you most curious about?

You might also like