0% found this document useful (0 votes)
4 views

Week-1-Chapter-2-JAVA

The document provides an introduction to Java syntax, including the structure of a Java program, the importance of the main() method, and how to print text and numbers using println() and print() methods. It also covers variable declaration, types of variables, and how to display variables in output. Additionally, it explains the use of comments and the final keyword for constant variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week-1-Chapter-2-JAVA

The document provides an introduction to Java syntax, including the structure of a Java program, the importance of the main() method, and how to print text and numbers using println() and print() methods. It also covers variable declaration, types of variables, and how to display variables in output. Additionally, it explains the use of comments and the final keyword for constant variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Syntax

In the previous chapter, we created a Java file called Main.java, and we used the
following code to print "Hello World" to the screen:

Main.java

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

Example explained
Every line of code that runs in Java must be inside a class. In our example, we
named the class Main. A class should always start with an uppercase first
letter.

Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.

The name of the java file must match the class name. When saving the file,
save it using the class name and add ".java" to the end of the filename. To run
the example above on your computer, make sure that Java is properly installed:
Go to the Get Started Chapter for how to install Java. The output should be:

Hello World

The main Method


The main() method is required and you will see it in every Java program:

public static void main(String[] args)

Any code inside the main() method will be executed. Don't worry about the
keywords before and after main. You will get to know them bit by bit while
reading this tutorial.
For now, just remember that every Java program has a class name which must
match the filename, and that every program must contain the main() method.

System.out.println()
Inside the main() method, we can use the println() method to print a line of text
to the screen:

public static void main(String[] args) {

System.out.println("Hello World");

Java Output / Print

Print Text
You learned from the previous chapter that you can use the println() method to
output values or print text in Java:

ExampleGet your own Java Server


System.out.println("Hello World!");

You can add as many println() methods as you want. Note that it will add a new
line for each method:

Example
System.out.println("Hello World!");

System.out.println("I am learning Java.");

System.out.println("It is awesome!");
Double Quotes
When you are working with text, it must be wrapped inside double quotations
marks "".

If you forget the double quotes, an error occurs:

Example
System.out.println("This sentence will work!");

System.out.println(This sentence will produce an error);

The Print() Method


There is also a print() method, which is similar to println().

The only difference is that it does not insert a new line at the end of the output:

Example
System.out.print("Hello World! ");

System.out.print("I will print on the same line.");

Java Output Number


Print Numbers
You can also use the println() method to print numbers.

However, unlike text, we don't put numbers inside double quotes:

ExampleGet your own Java Server


System.out.println(3);
System.out.println(358);

System.out.println(50000);

You can also perform mathematical calculations inside the println() method:

Example
System.out.println(3 + 3);

Example
System.out.println(2 * 5);

Java Comments
Comments can be used to explain Java code, and to make it more readable. It
can also be used to prevent execution when testing alternative code.

Single-line Comments
Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by Java (will not be
executed).

This example uses a single-line comment before a line of code:

ExampleGet your own Java Server


// This is a comment

System.out.println("Hello World");
This example uses a single-line comment at the end of a line of code:

Example
System.out.println("Hello World"); // This is a comment

Java Variables
Variables are containers for storing data values.

In Java, there are different types of variables, for example:

 String - stores text, such as "Hello". String values are surrounded by


double quotes
 int - stores integers (whole numbers), without decimals, such as 123 or -
123
 float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
 char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
 boolean - stores values with two states: true or false

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

SyntaxGet your own Java Server


type variableName = value;

Where type is one of Java's types (such as int or String), and variableName is
the name of the variable (such as x or name). The equal sign is used to
assign values to the variable.

To create a variable that should store text, look at the following example:
Example
Create a variable called name of type String and assign it the value "John":

String name = "John";

System.out.println(name);

To create a variable that should store a number, look at the following example:

Example
Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;

System.out.println(myNum);

You can also declare a variable without assigning the value, and assign the
value later:

Example
int myNum;

myNum = 15;

System.out.println(myNum);

Note that if you assign a new value to an existing variable, it will overwrite the
previous value:

Example
Change the value of myNum from 15 to 20:

int myNum = 15;

myNum = 20; // myNum is now 20

System.out.println(myNum);
Final Variables
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant", which
means unchangeable and read-only):

Example
final int myNum = 15;

myNum = 20; // will generate an error: cannot assign a value to a final


variable

Other Types
A demonstration of how to declare variables of other types:

Example
int myNum = 5;

float myFloatNum = 5.99f;

char myLetter = 'D';

boolean myBool = true;

String myText = "Hello";

Java Print Variables


❮ PreviousNext ❯
Display Variables
The println() method is often used to display variables.

To combine both text and a variable, use the + character:

ExampleGet your own Java Server


String name = "John";

System.out.println("Hello " + name);

You can also use the + character to add a variable to another variable:

Example
String firstName = "John ";

String lastName = "Doe";

String fullName = firstName + lastName;

System.out.println(fullName);

For numeric values, the + character works as a mathematical operator (notice


that we use int (integer) variables here):

Example
int x = 5;

int y = 6;

System.out.println(x + y); // Print the value of x + y

From the example above, you can expect:

 x stores the value 5


 y stores the value 6
 Then we use the println() method to display the value of x + y, which
is 11

You might also like