Gener B. Beralde: Instructor
Gener B. Beralde: Instructor
GENER B. BERALDE
PRE TEST
1) The default value of a static integer variable of a class in Java is,
(a) 0 (b) 1
(c) Garbage value (d) Null (e) -1.
2) What will be printed as the output of the following program?
public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println(“I = ” +i);
}
}
(a) I = 0 (b) I = 1
(c) I = 2 (d) I = 3 (e) Compile-time Error.
3) Multiple inheritance means,
(a) 1 , 3 (b) 3 , 1
(c) 1 , 1 (d) 1 , 0 (e) none of the above.
(a) A finally block is executed before the catch block but after the try block.
(b) A finally block is executed, only after the catch block is executed.
(c) A finally block is executed whether an exception is thrown or not.
(d) A finally block is executed, only if an exception occurs.
(e) None of the above.
What is Java?
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
Is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety
of platforms, such as Windows, Mac OS, and the various versions of UNIX.
It is used for:
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering
development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
To check if you have Java installed on a Windows PC, search in the start bar for Java or type the following in Command
Prompt (cmd.exe):
If Java is installed, you will see something like this (depending on version):
If you do not have Java installed on your computer, you can download it for free at oracle.com.
1. Go to "System Properties" (Can be found on Control Panel > System and Security > System > Advanced System
Settings)
2. Click on the "Environment variables" button under the "Advanced" tab
3. Then, select the "Path" variable in System variables and click on the "Edit" button
4. Click on the "New" button and add the path where Java is installed, followed by \bin. By default, Java is installed in
C:\Program Files\Java\jdk-11.0.1 (If nothing else was specified when you installed it). In that case, You will have to
add a new path with: C:\Program Files\Java\jdk-11.0.1\bin
Then, click "OK", and save the settings
5. At last, open Command Prompt (cmd.exe) and type java -version to see if Java is running on your machine
Step 5
Write the following in
the command line (cmd.exe):
If Java was successfully installed, you will see something like this (depending on version):
Basic Syntax
About Java programs, it is very important to keep in mind the following points.
• Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
• Class Names − For all class names the first letter should be in Upper Case. If several words are used to form a name
of the class, each inner word's first letter should be in Upper Case.
Example: class MyFirstJavaClass
• Method Names − All method names should start with a Lower Case letter. If several words are used to form the name
of the method, then each inner word's first letter should be in Upper Case.
Example: public void myMethodName()
• Program File Name − Name of the program file should exactly match the class name.
When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to
the end of the name (if the file name and the class name do not match, your program will not compile).
But please make a note that in case you do not have a public class present in the file then file name can be different
than class name. It is also not mandatory to have a public class in the file.
Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'
• public static void main(String args[]) − Java program processing starts from the main() method which is a mandatory
part of every Java program.
Java Identifiers
All Java components require names. Names used for classes, variables, and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows −
• All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
• After the first character, identifiers can have any combination of characters.
• A key word cannot be used as an identifier.
• Most importantly, identifiers are case sensitive.
• Examples of legal identifiers: age, $salary, _value, __1_value.
• Examples of illegal identifiers: 123abc, -salary.
When we consider a Java program, it can be defined as a collection of objects that communicate via invoking each other's
methods. Let us now briefly look into what do class, object, methods, and instance variables mean.
• Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such
as wagging their tail, barking, eating. An object is an instance of a class.
• Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type
supports.
• Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are
written, data is manipulated and all the actions are executed.
• Instance Variables − Each object has its unique set of instance variables. An object's state is created by the values
assigned to these instance variables.
Let us look at a simple code that will print the words Hello World.
Example
Live Demo
Output
C:\> javac MyFirstJavaProgram.java
C:\> java MyFirstJavaProgram
Hello World
Java Modifiers
Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers
−
• Access Modifiers − default, public , protected, private
• Non-access Modifiers − final, abstract, strictfp
We will be looking into more details about modifiers in the next section.
Java Variables
• Local Variables
• Class Variables (Static Variables)
• Instance Variables (Non-static Variables)
Java Keywords
The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any
other identifier names.
volatile while
Comments in Java
Java supports single-line and multi-line comments very similar to C and C++. All characters available inside any comment are
ignored by Java compiler.
Example
public class MyFirstJavaProgram {
Output
In Java, ever y application begins with a class name, and that class must match the filename.
Let's create our first Java file, called MyClass.java, which can be done in any text editor (like Notepad).
The file should contain a "Hello World" message, which is written with the following code:
MyClass.java
public class MyClass {
System.out.println("Hello World");
Run example »
Don't worry if you don't understand the code above - we will discuss it in detail in later chapters. For now, focus on how to
run the code above.
Save the code in Notepad as "MyClass.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved
your file, and type "javac MyClass.java":
This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type
"java MyClass" to run the file:
Hello World
Java Syntax
In the previous chapter, we created a Java file called MyClass.java, and we used the following code to print "Hello World" to
the screen:
MyClass.java
System.out.println("Hello World");
Run example »
Example explained
Every line of code that runs in Java must be inside a class. In our example, we named the class MyClass. A class should always
start with an uppercase first letter.
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 is required and you will see it in every Java program:
Any code inside the main() method will be executed. You don't have to understand 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:
System.out.println("Hello World");
Run example »
Note: The curly braces {} marks the beginning and the end of a block of code.
Exercise 1:
Insert the missing part of the code below to output "Hello World".
. . ("Hello World");
Exercise 2:
Comments in Java are written with special characters. Insert the missing parts:
Exercise 3:
Write a Java program to print 'Hello' on screen and then print your name on a separate line.
Expected Output :
Hello
GENER BERALDE