You are on page 1of 4

Computer Programming II

I. Introduction
1. Introduction and Installation
You must first install the JDK (Java SE Development Kit) from www.oracle.com to run
Java programs and develop applications for Java. After this, install the NetBeans, a free
and open source IDE (Integrated Development Environment) to quickly and easily
develop applications with Java.
2. What Java Is and How It Works
Java is one of the most used programming language. To develop with Java, you first
create a text file in .java file extension. You may use a specialized editor to create this
text file or you may use any kind of editor like Notepad. To turn your Java file into
binary files that the computer can read, you use javac, a computer program that is part
of the JDK you installed earlier. However, you do not have to worry about using the
javac directly (but you may, if you want to learn about that). Usually, you write your Java
programs in NetBeans (or any other IDE for Java), and then click on the Run button to
create the binary file (this uses the javac behind-the-scene). This binary file will have the
extension .class. This .class will then be read and run by the Java Virtual Machine (JVM)
running on your device.
II. Programming Core Java
1. Hello World Program
In NetBeans, to create a Java program, you must first create a Java project by clicking
the File->New->Project->Java->Java Application->Next->Name you project anything you
like->Browse for the project location->Uncheck first the Create Main Class option.
In the Project Explorer, usually located on the left, right-click on the created Java
Project, choose New->Java Class. Give your new class a name, for example, HelloWorld
(Note: should not contain spaces and should start with capital letter), and package, for
example, main (Note: your package name should start with lowercase letter).
This will generate a .java file with your class name.
To start, you must first write the main method
public static void main(String[] args){

}
inside your class. This method is the starting point of your Java program. Next, to print
the message Hello, World! to the console, write

System.out.println(“Hello, World!”);

inside the main method. Every statement in you program should end with a semicolon
(;).
Your complete program should look like this:

public class HelloWorld{


public static void main(String[] args){
System.out.println(“Hello, World!”);
}
}
After this, look for the green Run icon to run your program.
2. Using Variables
One of the most basic building blocks of any computer program is declaring and
initializing variables. A variable is like a storage box that you can put anything on it.
Example of declaring a variable:

int myInteger;

myInteger is like the name of your storage box (which we call variable) and the int is like
what kind of things you are allowed to store on that box (called data type).
There are different data types you may use in a program.

Data Type Size Usage/ Value Range


byte 8-bit signed integer -128 up to 127 (inclusive)
This is useful for saving
memory.
short 16-bit signed integer -32,768 up to 32,767
(inclusive)
This is also useful for saving
memory.
int 32-bit signed integer Default: -231 up to 231-1
In Java SE 8 and later, you
can use the Integer class to
use int data type as
unsigned, ranging from 0 to
232-1
long 64-bit integer Signed: -263 up to 263-1
Unsigned: 0 up to 264-1
float 32-bit floating point Use a float (instead of
double) if you need to save
memory
double 64-bit floating point This data type should never
be used for precise values,
such as currency.
boolean true and false
Use this data type for simple
flags that track true/false
conditions
char single 16-bit Unicode Storing single alphanumeric
character or symbol character. Enclose
with single quotation mark,
i.e ‘a’
String (actually a class, not Storing string of characters
a primitive type) (one or more characaters).
Enclose with double
quotation mark, i.e. “a1b2”

To initialize a variable is to assign a value to it (putting designated thing to the box).


You use the single equal (=) sign to assign a value. We call this symbol, i.e. =, assignment
operator because we use it to “assign” a value to a variable.
To print the value of a variable, do not enclose the name of the variable with quotation
marks inside the System.out.println method.

Write the following program to practice declaring, initializing, and printing variables.

public class Application {

public static void main(String[] args) {


int myNumber = 88;
short myShort = 847;
long myLong = 9797;

double myDouble = 7.3243;


float myFloat = 324.3f;

char myChar = 'y';


boolean myBoolean = false;

byte myByte = 127;

System.out.println(myNumber);
System.out.println(myShort);
System.out.println(myLong);
System.out.println(myDouble);
System.out.println(myFloat);
System.out.println(myChar);
System.out.println(myBoolean);
System.out.println(myByte);
}

Output:
88
847
9797
7.3243
324.3
y
false
127

3. Strings: Working with Text

public class Application {


public static void main(String[] args) {
int myInt = 7;
String text = "Hello";
String blank = " ";
String name = "Bob";
String greeting = text + blank + name;
System.out.println(greeting);
System.out.println("Hello" + " " + "Bob");
System.out.println("My integer is: " + myInt);
double myDouble = 7.8;
System.out.println("My number is: " + myDouble + ".");
}
}

You might also like