Java Fundamentals l «Prev  Next»


Lesson 2Source files and compilation units
Objective Relationship between Java programs, source files, and compilation units

Java Source Files and compilation units

Create and use switch statement

You can use a switch statement to compare the value of a variable with multiple values. For each of these values, you can define a set of statements to execute. The following example uses a switch statement to compare the value of the variable marks with the literal values 10, 20, and 30, defined using the case keyword:
int marks = 20;
switch (marks) {
 case 10: System.out.println(10);
  break;
 case 20: System.out.println(20);
  break;
 case 30: System.out.println(30);
  break;
 default: System.out.println("default");
  break;
}
A switch statement can define multiple case labels within its switch block, but only a single default label. The default label executes when no matching value is found in the case labels. A break statement is used to exit a switch statement, after the code completes its execution for a matching case.

Comparing a switch statement with multiple if-else constructs

A switch statement can improve the readability of your code by replacing a set of (rather complicated-looking) related if-else-if-else statements with a switch and multiple case statements.
Examine the following code, which uses if-else-if-else statements to check the value of a String variable day and display an appropriate message:
String day = "SUN";
if (day.equals("MON") || day.equals("TUE") 
|| day.equals("WED") || day.equals("THU"))
  System.out.println("Time to work");
else if (day.equals("FRI"))
  System.out.println("Nearing weekend");
else if (day.equals("SAT") || day.equals("SUN"))
  System.out.println("Weekend!");
else
  System.out.println("Invalid day?");

Now examine this implementation of the previous logic using the switch statement:
String day = "SUN";
switch (day) {
 case "MON":
 case "TUE":
 case "WED":
 case "THU": System.out.println("Time to work");
  break;
 case "FRI": System.out.println("Nearing weekend");
  break;
 case "SAT":
 case "SUN": System.out.println("Weekend!");
  break;
 default: System.out.println("Invalid day?");
}

The two previous snippets of code perform the same function of comparing the value of the variable day and printing an appropriate value. But the latter code, which uses the switch statement, is simpler and easier to read and follow. Note that the previous switch statement does not define code for all the case values. What happens if the value of the variable day matches TUE?
When the code control enters the label matching TUE in the switch construct, it’ll execute all of the code until it encounters a break statement or it reaches the end of the switch statement.

Source code files on the Java Certification exam

You will see questions on the certification exam that will present one or more sample source code files. The question will ask you to identify which source code file compiles without errors. Make sure that the source code file does not contain more than one public class or interface. Also, make sure that the name of the source code file matches that of the public class or interface.
public class Switch2{
 final static short x=2;
 final static int y = 0;
 public static void main (String [] args){
  for (int z=0; z < 4; z++){
   switch (z){
    case x: System.out.println ("0");
    case x-1: System.out.println("1"); break;
    default: System.out.println("def");
    case x-2: System.out.println("2");
   }
  }
 }
}

Describe the relationship between Java programs, source files, and compilation units. You write Java programs by declaring classes and interfaces in source code files, referred to as compilation units.
Classes are the cornerstone of Java programs. They declare variables, methods, and constructors .
  1. Variables are used to store the values of primitive types and provide references to objects .
  2. Methods are used to access variables and perform operations on them.
  3. Constructors are used to create objects, which are instances of classes.

Interfaces define constants and methods. For a class to implement a particular interface, it must implement all of the methods that are defined in the interface. A class can define any constant whether or not the constants are already defined in the interfaces implemented by the class.

The figure above describes the concept of compilation units for classes and interfaces.
The figure above describes the concept of compilation units for classes and interfaces.


Java Compilation Unit

It is basically referred to a Java source code file, which forms the input for the Java compiler which is javac. This term is less commonly used among developers and it is technical jargon for Java source code file. This Java compilation unit has structure which is as follows
[ package declaration ] 
[ import declararion/s or static import declaration/s]
[ top-level type declaration/s; ]

Some points about the compilation unit, these are somewhat based on compiler:
  1. may or may not contain the above components
  2. if anyone of them present, then must go in selected slots and follow order as shown above, such as, package declaration should be before any of type declaration
  3. can contain the one or more type declaration must go in the order as shown above, type in this case can be class, interface, enum or annotation
  4. if it contains public access level type then it must be only one and can have no. of non-public/default level types, In other words, it can contain '1' public type and any 'n' non-public types
  5. name of compilation unit can be, name of the public type, if present or any type name inside it or anything you like provided no public type are present inside it
  6. non-javadoc comments can go anywhere, does not necessary follow any order as shown above structure.
Java source code files end with the .java extension and contain at least one class or interface declaration. A source code file may declare, at most, one public class or interface. If a source code file declares a public class or interface, the file's name must match that of the class or interface.

SEMrush Software