Java Tutorial: System Requirements

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

JAVA TUTORIAL

System Requirements
Install the following:
1. DOWNLOAD JAVA SE DEVELOPMENT A INSTALL JAVA JRE
2. SELECT AN IDE
For Eclipse user, if error Unbound classpath container appeared
after creating the Project, pls configure the project properties:
 Right click on the project
 Open Properties
 Select Java Build Path
 Select the JRE System Library under the Libraries tab.
 Click on Edit.
 Click on Alternate JRE.
 If Alternate JRE present, then Select it and hit Finish.
 If not present, then click on Installed JREs which opens a
pop up.
 Click on Add.
 Select Standard VM and Click Next.
 Select the JRE Home, navigate to JDK root and select that.
 Click on Finish.
 Select that in installed JREs.

Hello World
To start with, right click on the created project, then hover to
New, select Class. Name the class with a unique name, and then
check the method “public static void main (String[] args)”, then
hit Finish.
public class Main //this is the "main" class
{

public static void main(String[] args) //this is the "main" method

{
System.out.println("Hello World");
System.out.println("Good Morning Philippines!");
}
}

By using the “println” statement, the output will divide the


results (strings) into two separate lines like this:
RESULT:

However, if we will change the “println” statement into “print”,


the results will display in a single line or sentence only, like
this:
System.out.print("Hello World");
System.out.print("Good Morning Philippines!");

RESULT:

The same “println” result may obtain also by adding “\n” at the
end of the string statement, like this:

System.out.print("Hello World\n");
System.out.print("Good Morning Philippines!");

RESULT:

Another interesting thing is that we can use the same “println”


statement and \n in the same program, and then we will have this
result:

System.out.println("Hello World\n");
System.out.println("Good Morning Philippines!");

RESULT:

Tab
To put a tab before the string result, we can add “\t” before the
string, like this:

System.out.println("\tHello World");
System.out.println("Good Morning Philippines!");

RESULT:
Quotation Mark
To add quotation mark in an output, we can place “\ before and
after the sentence, like this:
System.out.println("\t \"Hello World\"");
System.out.println("Good Morning Philippines!");

RESULT:

Adding Backslash
To add a backslash (\) into the string result, you can just add
two backslashes (\\) before the strings, like this:
System.out.println("\\ Hello World");

RESULT:

Adding Comments
Comments are characters or statements that could be ignored by
the compiler. This is in case you want to put something in the
program that you don’t want the compiler to display on the
console, like this:
System.out.println("\\ Hello World");
// This is a comment

RESULT:
You will notice that the compiler ignored the comment line and
didn’t display it on the console. This will happen if we added
two backslashes (\\) in a new or empty space in the program.
However, if we want to have a multiple comments, we can use
forward slash and asterisk (/*), like this:
{
System.out.println("\\ Hello World");
/* Pls do not
* make any
* changes in this program
*/
}

RESULT:

Variables
Variables are being described by its data type. There are at
least 9 primitive data types that can be used in Java.
Boolean = true /false
Byte = -128 to 127
Short = 32,768 - 32,767
Int = -2 billion to 2 billion
Long = -9 quintillion to 9 quintillion
Float = fractional number up to 6-7 digits ex. 3.141592f
Double = fractional number up 15 digits ex. 3.141592653589793
Char = single characted/letter/ASCII value
ex. 'f'
String = a sequence of characters ex. "Hello World"

To use variables in a program, please check samples below:


public class Main {

public static void main(String[] args)


{

int age;
int sum;
sum = 167 + 18;
age = 19;
float x = 12.15f;
double z = 12.15;
char sign = '&';
boolean z1 = false;

String name = "Jerome Macarilao";


System.out.println ("My name is " + name + " I am " + age + " years
old.");
System.out.println (age + sum);
System.out.println (x);
System.out.println(z);
System.out.println(sign);
System.out.println(z1);
}

Result:
My name is Jerome Macarilao I am 19 years old.
204
12.15
12.15
&
False

Whenever we concatenate the variables, we have to use the (+)


sign, like this:
System.out.println ("My name is " + name + " I am " + age + " years old.");

Scanner
Scanner is a command that allows us to input a data and interact
with the program, just like the cin in C++. Before we use this
command, we have to include the following line into our package:
import java.util.Scanner;

Example:
import java.util.Scanner;

public class Main {

public static void main(String[] args)


{
Scanner x = new Scanner (System.in);

System.out.println ("What is you name?");


String name = x.nextLine();

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

Once we run this program, it will display a question. Then it


will pause for a while until you give a response by typing your
answer. Once you input the answer, then it will generate a new
message which includes your answer, and that’s how the program
scans.
to be continue from 39:21

You might also like