Introduction To Java
Introduction To Java
Java
SYNTAX
Every line of code that runs in Java must be
inside a class. In the example above, we named
the class Main. A class should always start with
an uppercase first letter.
The main() method is required and you will see it in every Java
program:
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. 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:
Reminders:
The curly braces { } marks the beginning and the end
of a block of code.
System is a built-in Java class that contains useful
members, such as out, which is short for "output".
The println() method, short for "print line", is used
to print a value to the screen (or a file).
Don't worry too much about System, out and
println(). Just know that you need them together to
print stuff to the screen.
You should also note that each code statement must
end with a semicolon (;).
Using the above syntax as a guide, code the
following output:
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null,
"Welcome to Java Programming");
System.exit(0);
}
}
Besides allowing you to use the System class to produce command window output, Java provides
built-in classes that produce GUI output on dialog boxes. A dialog box is a GUI object resembling a
window which you can place messages you want to display.
You use an import statement when you want to access a built-in Java class that is contained in a
group of classes called package. To use the JOptionPane class, you must import the package named
javax.swing.JoptionPane.
In older versions of Java, any application that use a dialog was required to end with System.exit(0);
statement or the application would not terminate. You can add this statement to your programs, and
they will work correctly, but it is not necessary.
SCANNER CLASS METHODS
Method Description
nextDouble() Retrieve input as double
nextInt() Retrieves input as an int
nextLine()
Retrieves the next line of data and returns it as a String
next()
Retrieves the next complete token as a String
COMPLETE THE MISSING CODE AND RUN
IT import javax.swing.JOptionPane;
import java.util.Scanner;
Here, age is a local variable. This is defined inside pupAge() method and its
scope is limited to only this method.
Java Instance Variables
Instance variables are declared in a class, but outside a
method, constructor or any block.
When a space is allocated for an object in the heap, a slot
for each instance variable value is created.
Instance variables are created when an object is created
with the use of the keyword 'new' and destroyed when the
object is destroyed.
Instance variables hold values that must be referenced by
more than one method, constructor or block, or essential
parts of an object's state that must be present throughout
the class.
Instance variables can be declared in class level before or
after use.
Access modifiers can be given for instance variables.
The instance variables are visible for all methods,
constructors and block in the class. Normally, it is
recommended to make these variables private (access
level). However, visibility for subclasses can be given for
these variables with the use of access modifiers.
Instance variables have default values. For numbers, the
default value is 0, for Booleans it is false, and for object
references it is null. Values can be assigned during the
declaration or within the constructor.
Instance variables can be accessed directly by calling the
variable name inside the class. However, within static
methods (when instance variables are given accessibility),
they should be called using the fully qualified
name. ObjectReference.VariableName.