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

Gener B. Beralde: Instructor

The document provides an overview of Java programming language including: - Java was created in 1995 and is owned by Oracle. It is a popular programming language used widely for mobile apps, web apps, games and more. - Key reasons for using Java include its ability to run on multiple platforms, popularity, ease of use, security, speed and large community support. - The document outlines how to set up Java on Windows, check if it is already installed, and provides examples of basic Java syntax including classes, objects, methods and variables. - It explains some core Java concepts such as identifiers, the main method, and provides a simple "Hello World" example program.

Uploaded by

Xander Braga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Gener B. Beralde: Instructor

The document provides an overview of Java programming language including: - Java was created in 1995 and is owned by Oracle. It is a popular programming language used widely for mobile apps, web apps, games and more. - Key reasons for using Java include its ability to run on multiple platforms, popularity, ease of use, security, speed and large community support. - The document outlines how to set up Java on Windows, check if it is already installed, and provides examples of basic Java syntax including classes, objects, methods and variables. - It explains some core Java concepts such as identifiers, the main method, and provides a simple "Hello World" example program.

Uploaded by

Xander Braga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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) one class inheriting from more super classes


(b) more classes inheriting from one super class
(c) more classes inheriting from more super classes
(d) None of the above
(e) (a) and (b) above.

4) Which statement is not true in java language?

(a) A public member of a class can be accessed in all the packages.


(b) A private member of a class cannot be accessed by the methods of the same class.
(c) A private member of a class cannot be accessed from its derived class.
(d) A protected member of a class can be accessed from its derived class.
(e) None of the above.

5) To prevent any method from overriding, we declare the method as,

(a) static (b) const


(c) final (d) abstract (e) none of the above.

6) Which one of the following is not true?

(a) A class containing abstract methods is called an abstract class.


(b) Abstract methods should be implemented in the derived class.
(c) An abstract class cannot have non-abstract methods.
(d) A class must be qualified as ‘abstract’ class, if it contains one abstract method.
(e) None of the above.

7) The fields in an interface are implicitly specified as,


(a) static only (b) protected
(c) private (d) both static and final (e) none of the above.

8) What is the output of the following program:

public class testmeth


{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+” , “);
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}

(a) 1 , 3 (b) 3 , 1
(c) 1 , 1 (d) 1 , 0 (e) none of the above.

9) Which of the following is not true?

(a) An interface can extend another interface.


(b) A class which is implementing an interface must implement all the methods of the interface.
(c) An interface can implement another interface.
(d) An interface is a solution for multiple inheritance in java.
(e) None of the above.

10) Which of the following is true?

(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:

• Mobile applications (specially Android apps)


• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection

Why Use Java?

• 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

Java Getting Started

Some PCs might have Java already installed.

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):

Java Install C:\Users\Your Name>java -version

If Java is installed, you will see something like this (depending on version):

java version "11.0.1" 2018-10-16 LTS


Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

If you do not have Java installed on your computer, you can download it for free at oracle.com.

Setup for Windows

To install Java on Windows:

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):

C:\Users\Your Name>java -version

If Java was successfully installed, you will see something like this (depending on version):

java version "11.0.1" 2018-10-16 LTS


Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

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.

First Java Program

Let us look at a simple code that will print the words Hello World.

Example
Live Demo

public class MyFirstJavaProgram {

/* This is my first java program.


* This will print 'Hello World' as the output
*/

public static void main(String []args) {


System.out.println("Hello World"); // prints Hello World
}
}
Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −
• Open notepad and add the code as above.
• Save the file as: MyFirstJavaProgram.java.
• Open a command prompt window and go to the directory where you saved the class. Assume it's C:\.
• Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there are no errors in your code, the
command prompt will take you to the next line (Assumption : The path variable is set).
• Now, type ' java MyFirstJavaProgram ' to run your program.
• You will be able to see ' Hello World ' printed on the window.

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

Following are the types of variables in Java −

• 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.

abstract assert boolean break

byte case catch char

class const continue default

do double else enum

extends final finally float

for goto if implements


import instanceof int interface

long native new package

private protected public return

short static strictfp super

switch synchronized this throw

throws transient try void

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 {

/* This is my first java program.


* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/

public static void main(String []args) {


// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}

Output

Java Quick start

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 {

public static void main(String[] args) {

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":

C:\Users\Your Name>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:

C:\Users\Your Name>java MyClass

The output should read:

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

public class MyClass {

public static void main(String[] args) {

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.

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. 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:

public static void main(String[] args) {

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

Run example »

Note: The curly braces {} marks the beginning and the end of a block of code.

Note: Each code statement must end with a semicolon.

Exercise 1:

Insert the missing part of the code below to output "Hello World".

public class MyClass {

public static void main(String[] args) {

. . ("Hello World");

Exercise 2:

Comments in Java are written with special characters. Insert the missing parts:

This is a single-line comment


This is a multi-line comment

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

You might also like