Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Introduction To Java

Uploaded by

Axel Mendoza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Introduction To Java

Uploaded by

Axel Mendoza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to

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.

 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.
The Main Method

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:

My name is Juan Dela Cruz


From BSIT-Block
GUI OUTPUT USING JOptionPane

import javax.swing.JOptionPane;

public class GUISample1 {


public static void main(String args[]) {

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;

public class GetUserInput {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name>> ");
String name = input.nextLine();
JOptionPane.showMessageDialog(null, "Your
name is " + name);
}
}
What is a Java Variable?

A variable provides us with named storage


that our programs can manipulate. Each
variable in Java has a specific type, which
determines the size and layout of the
variable's memory; the range of values that
can be stored within that memory; and the
set of operations that can be applied to the
variable.
Variable Declaration and Initialization
You must declare all variables before they can be
used. Following is the basic form of a variable
declaration −

Here data type is one of Java's data types


and variable is the name of the variable. To
declare more than one variable of the specified
type, you can use a comma-separated list.
Example of Valid Variables Declarations and
Initializations

 Following are valid examples of variable declaration and initialization in Java


Java Variables Types

The following are the three types of


Java variables:
Local variables
Instance variables
Class/Static variables
1. Java Local Variables
 Local variables are declared in methods, constructors, or
blocks.
 Local variables are created when the method, constructor
or block is entered and the variable will be destroyed
once it exits the method, constructor, or block.
 Access modifiers cannot be used for local variables.
 Local variables are visible only within the declared
method, constructor, or block.
 Local variables are implemented at stack level internally.
 There is no default value for local variables, so local
variables should be declared and an initial value should be
assigned before the first use.
Example 1: Variable's local scope with initialization

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

You might also like