Data Type & Scanner

Before every program, don't forget to import the scanner class, by typing " import java.util.Scanner; " at the top left corner.
In addition to that, inside the main function, we need to declare a Scanner variable and create and instance of the Scanner class.

 

Declaring  Variables

In java, we must explicitly declare all variables before using them. The basic form of a variable declaration is this:

type name;

Example)

int x;
String name;
double area;

In the examples above variable named x, name, and area are declared. The x variable can hold integer values,
the name variable holds String values, and the area holds double values. (Note variable declaration also ends with a semicolon).
We can declare two or more variables of the same type in a single statement, by separating the variable name.

Example)

int x, y, z;
double a, b, c;

Declaring Class Variables

A class variable is a variable that any method in a class can access. Here are two basic rules for declaring a class variable.

        1)    The declaration must be within the body of the class and not within any of the class methods.
       
        2)    It must include the word static in the declaration. The word static comes before the variable.

Example)

public class HelloWorld
{
    static String hello;    // Class variable declaration

    public static void main(String[] args)
    {
        hello = "Hello World";
        System.out.println(hello);
    }
}

As we can see from the example above, the key word static is placed before String and
the declaration is outside of main.

 

Declaring Local Variables

A local variable is a variable that's declared within the body of a method. Then we can
only use the variable within the method. Other methods in the class wouldn't be able to use it.

 

Example)

public class HelloWorld
{

    public static void main(String[] args)
    {
        String hello;
        hello = "Hello World";
        System.out.println(hello);
    }
}

Note that we declare String hello inside our main method and we don't use the static key word or else we would get an error.

Initializing Variables with initializers

In java, we are allow to initialize a variable on the same statement that declares the variable.
In addition, we can declare more than one variable in a single statement. To do that, we need
to use an initializer, which has the following general form:

type name = expression;

Example)

int x = 0;
String name = "Jay";
double area = 0.0;

Example)

int x = 4, y = 5;
 

When we declare two class variables in a single statement but use only one initializer,
we would  think that the initializer applies to both variables.

Example)

static int x, y = 5;

Here, both x and y are not initialize to 5.

 

Using final Variables

A final variable, which also called constant, is a variable whose value that we can't change once it's declared.

Example)

static final int x = 5;

Example)

final int x = 5;

Working with Strings

A string is a sequence of text characters, such as the message " Hello World".
We are able to combine strings using a plus sign ( + ) as a concatenation operator.
( Concatenation is just a nerdy term in java, meaning combining two strings )

 

Enough with the definitions and small examples, let see some real programming examples.

 

/*    The program will ask the user to enter two numbers, then it will find the sum of the two numbers
        It will output the two numbers entered by the user, and the sum of the two numbers.

*
*/

import java.util.Scanner;    //needed for the scanner class

public class SumNumber
{
        public static void main(String[] args)
        {

                int x, y;    //Now x and y can hold integers
                int sum = 0;    // Set sum as 0

                Scanner key = new Scanner(System.in);    // Creating the Scanner object

                System.out.println("Enter the first integer: ");
                x = key.nextInt();                                            //Store the first integer into x

                System.out.println("Enter the second integer: ");
                y = key.nextInt();                                            //Store the second integer into y


                sum = x + y;                //adds up the first and second integers then store it into sum

                System.out.println("The integers you entered are " + x + " and " + y);    //Outputs the integers entered by the user
                System.out.println("The sum of the integer is " + sum);                        //Outputs the sum of the two integers

        }

}

 

ClickHere to download SumNumber.java

 

/* The program asks the user to input their first and last name
Then it prints out it's last name and first name with a greeting.
It's a simple program using String

*
*/

import java.util.Scanner; //Needed for the Scanner class

public class Name
{
    public static void main(String[] args)
    {
        String first, last; // Declare first and last as a string

        Scanner key = new Scanner(System.in); // Scanner object

        System.out.print("Please enter your first name: "); //Get the user's first name
        first = key.nextLine();

        System.out.print("\nPlease enter your last name: "); //Get the user's last name
        last = key.nextLine();

        //Outputs their last & first name with a greeting

        System.out.println("Hi "+last +" " + first + ", how are you doing today");
    }
}

 

ClickHere to download Name.java
 

/* This is a simple program that uses class variable & local variable
It asks the user for first and last name then output their monthly salary

*
*/

import java.util.Scanner; // Needed for scanner class

public class Salary
{
    static final double pay = 15.50; // Pay is a constatnt class variable

    public static void main(String[] args)
    {
        String first, last;
        final int hours = 20; // Hours is a constant integer
        double sum = 0.0;

        Scanner key = new Scanner(System.in);

        System.out.print("Enter your first name: ");
        first = key.nextLine();

        System.out.print("Enter your last name: ");
        last = key.nextLine();

        sum = hours * pay; // Calculates the monthly salary

        System.out.println(first + " " + last + " Your monthly salary is " + sum);
    }
}

ClickHere to download Salary.java

 

 

 

  <Java 24><Home>