You will implement some code to compare the value between different primitive and object.
For the different example, I am only showing you the example of the methods, you will need to write a main to test these methods. To respect the OOP principles, you should write a class that will contains your test.
Relational operator in Java
Source of the image (C. Horstamn: Big Java)
public void testSimpleEquality(){
int a = 5;
int b = 10;
System.out.println(" a == b "+ (a == b));
}
If you implement this method in a class, test this class the console will print "false", as a is not equal to b.
Relational operator have a lower precedence than the arithmetic operators, by consequence you could write:
public void testSimpleEqualityBalanceM1(){
int a = 5;
int b = 6;
int balance = a + b;
System.out.println(" a+b == balance "+ (a+b == balance));
}
This code will give the same result than this code
public void testSimpleEqualityBalanceM2(){
int a = 5;
int b = 6;
int balance = a + b;
System.out.println(" a+b == balance "+((a+b) == balance));
}
The class to test this code:
public class ConditionalTest{
public static void main (String[]args){
EqualityClass et = new EqualityClass();
et.testSimpleEquality();
et.testSimpleEqualityBalanceM1();
et.testSimpleEqualityBalanceM2();
System.out.println("\n");
}
}
All programming languages have defined a way of different actions depending of a condition. Java is no exception.
Java has two different syntax to define a condition, the if statement and the switch statement, you will first see the if statement.
The if statement of Java has two parts, the condition and the body. If the condition is true, the body of the statement will be executed.
The simple form of an if statement will be defined like this, if a equal to be, the body of the statement will be executed, if it is not equal, nothing will happen, and the virtual machine will pass to the next line.
I am implementing a new class ConditionalDemo.
public class ConditionalDemo{
public void testSimpleIf(int a, int b){
if (a == b){
System.out.println("a :" + a + " equal b :" + b);
}
System.out.println("end of the program");
}
}
In Java, if the body of the statement consists of only one statement, the {} can be omitted (small reminder, the use of indentation is only for human reading).
public void testSimpleIfOneLine(int a, int b){
if (a == b)
System.out.println("a equal b");
System.out.println("end of the program");
}
To test this class in your ConditionalTest class:
public class ConditionalTest{
public static void main (String[]args){
EqualityClass et = new EqualityClass();
et.testSimpleEquality();
et.testSimpleEqualityBalanceM1();
et.testSimpleEqualityBalanceM2();
System.out.println("\n");
ConditionalDemo cd = new ConditionalDemo();
System.out.println("testSimpleIf");
cd.testSimpleIf(2,2);
System.out.println("testSimpleIf");
cd.testSimpleIf(4,3);
}
}
In java, if you need to have an alternative action, you will define the else, and define in the else body the actions that need to be done.
public void testSimpleIfWithElse(int a, int b){
if (a == b){
System.out.println("a equal b");
}else{
System.out.println("a is not equal to b");
}
}
In java, if you need several alternative actions, you will define the else if, and define in the else if the actions that need to be done.
public void testSimpleIfWithElseIf(int a, int b, int c){
if (a == b){
System.out.println("a equal b");
}else if(a == c){
System.out.println("a equal b");
}else if(b == c){
System.out.println("a equal b");
}else{
System.out.println("no numbers are equal");
}
}
Test these two methods in your ConditionalTest class.
In Java, you have the if quick notation. The syntax if condition ? value1 : value2. The value of that expression is either value1 if the condition is true, or the value2 if the condition is false.
public int quickIfnotation(int y, int x){
int b = x > y ? x : y;
}
This quick notation could be useful, but it could also be difficult to read.
In Java, you have another syntax than the if to defined different actions depending of the result of the conditional test, it is the switch statement. It is often use when the sequence of if/else start to be too long. The advantage of the switch is obvious when the same variable need to be tested for different values.
The switch keyword is followed by the expression that will be test in the next lines. The body of the switch statement (switch block) start with the condition that should be true to run the body of code that is contained in this block. The switch block should be ended by a break, unless you want the next block to be run.
The type of variables you can test are: integers, characters, enumerations or strings. You cannot test floating-points value.
public void switchTest(int a) {
switch(a){
case 1: System.out.println("one");
break;
case 2: System.out.println("two");
break;
case 3: System.out.println("three");
break;
case 4: System.out.println("fourth");
break;
case 5: System.out.println("five");
break;
default: System.out.println("any numbers other than 1,2,3,4,5);
}
The test will be on the variable a (type integer) and depending of the result of the test, will print out the line that correspond to the result of the test.
public void switchDays(String day) {
switch(day){
case "Monday": System.out.println("Monday");
break;
case "Tuesday": System.out.println("Tuesday");
break;
case "Wednesday": System.out.println("Wednesday");
break;
default: System.out.println("The best day is Sunday");
}
String are not primitive, they are object. To test the value of two String, you cannot use the "==" operators, it will test if the object is the same, not the value of the object, you will need to use the .equal() method.
public boolean testEqualityBetweenTwoString(String s1, String s2){
if(s1.equals(s2)){
return true;
}else{
return false;
}
}
Mutable object can change after it's created. By default in Java, everything is mutable expect String, that are immutable. Immutable object cannot change value after it has been created. If you change the value of a String, Java create a new and separate copy of the String.
To optimise the amount of memory allocated to a String, the JVM store only one copy of each literal String in the pool. This process is called interning. When we create a String variable and assign a value to this variable, the JVM serarches in the pool if a String of equal value already exist, it is does find one, the Java compiler will return the reference to its memory address wihtout allocating additional memory. If the value is not find, the new value will be added to the pool, and the reference will be returned.
By adding this, the test using the operator "==" could work in some condition, as in fact it is testing the reference and not the value of it. However, it should never be used.
You can test the following code to understand this.
public void testingEquality(){
String a = new String("hello");
String b = "hello";
String d = "hello";
System.out.println( "(a == b) = " + (a == b) );
System.out.println( "(a.equals(b)) = " + (a.equals(b)) );
System.out.println( "(c == b) = " + (c == b) );
System.out.println( );
}
You can find the code for this example in https://git.cardiff.ac.uk/ASE_GROUP_2020/code_for_codelabs.git