Monthly Archives: November 2013

5 Minutes of Java (6. Using equals() or ==)

Using == for comparision

In python, to compare whether two variables have the same value, we will write the following code:

if (a == b):

print “a is equal to b”

This works whether the data type of the variables is String or number.

In Java, you can still use == to compare whether two numbers have the same value:
int a = 3;
int b = 3;
if (a == b){

 System.out.println(“a and b has the same value”);

}

However, you should use the equals() method to compare String in Java. In the code segment shown below, if you enter “hello” for both inputs, only the following result will be displayed: “Using equals() for comparison: a is equal to b”.

String a = Helper.readString("Enter first String");
String b = Helper.readString("Enter second String");

if(a==b){
 System.out.println("Using == for comparison: a is equal to b");
}

if(a.equals(b)){
 System.out.println("Using equals() for comparison: a is equal to b");
}

In summary,

  • to compare numbers in Java, use ==
  • to compare Strings in Java, use equals()

5 Minutes of Java (5. Decision Making with if/else statement)

Decision making is done using the if/else statement. See example below on the code to determine whether a person passed a test:

int score = 59;

if (score >= 50){
    System.out.println("You passed the test.");
}
else{
    System.out.println("Try harder next time.");
}

To handle more than 2 conditions, you will use the else if statement. This is similar to elif in python.

int score = 65;
char grade = 'F';

if (score > 70){
   grade = 'A';
}
else if (score >= 50){
   grade = 'C';
}
else{
    grade = 'F';
}

System.out.println("Your grade is " +  grade);

5 Minutes of Java (4. Java Methods)

In Python, you define function to create a block of re-usable code. In Java, this is done using method. You must create your method within the Java class.   There can be more than one method in each Java class.

The code below shows you the syntax of a Java method:

modifier returnValueDataType methodName(list of parameters) {
  // Method body;
}

The code below shows you how to create a method addNumber with the following attributes in a class MyNumber:

public class MyNumber{
    public double addNumber(double x, double y){
        return x + y;
    }
}
  1. public — Method modifier. It tells the compiler how to call the method. Always use public for now, you will learn more about this later on.
  2. double — The data type of the value returned by the method. In this example, x + y which is of double data type, will be returned.
  3. addNumber — Name of the method.
  4. double x, double y — The parameters that are passed to the method. In this example, there are 2 parameters: x and y. Both x and y have double data type.
  5. return x + y — The method body. The result of (x + y) is the value to be returned by the method. The data type must match those stated at point 2.

Take a look at another method given below:

public class MyName{
    public void sayHello(String name){
        System.out.println("Hello " + name);
    }
}
  1. void — This means that the method will not return any value. Take note that there is no “return …” in the method body.
  2. System.out.println(…) — The method body. It calls a method to print out the value in its parameter for the user to see.

 

5 Minutes of Java (3. User Input and Display Result)

Getting User Input

In Python, you get user input using the raw_input() function:

  • age = int(raw_input(“Enter your age: “))
  • weight = float(raw_input(“Enter your weight: “))
  • name = raw_input(“Enter your name: “))

In Java, getting user input is more complicated. We have provided a Helper class to simplify the process of getting user input. These are some of the methods that you will commonly use.

  • int age = Helper.readInt(“Enter your age: “);
  • double weight = Helper.readDouble(“Enter your weight: “);
  • String name = Helper.readString(“Enter your name: “);


Display Result

In python, the print() function is used to display result.

  • print (“Hello World”)

In Java, you will need to use the following function to display result.

  • System.out.println(“Hello World”);

 

5 Minutes of Java (2. Declaring and Initialising Variables)

Declaring and Initialising Variables

In Python, you can declare and initialise variables in the following formats:

  • age = 5
  • height = 1.75
  • name = “john”

Python is smart enough to know that the data type for age is int, height is float and name is str.

In Java, you declare and initialise variables in the following formats:

  • int age = 5;
  • double height = 1.75;
  • String name = “john”;

You have to state the data type of the variable as part of variable declaration. (Take note of the semi-colon at the end of each statement).

 One Important point to note:
For python, you can change the data type of a variable. For example,the same variable a can be used to store the value 1 (an integer) and the value “john” (a string).

This is not allowed in Java. If you have declared the variable a as an integer.

  • int a = 1;

You will get an error if you attempt to do the following:

  • a = “john”;