Three Facts About Java

In my day-to-day job, I program Java, which is a statically typed object-oriented language. It’s used in a lot of places and I primarily use it for building web applications. Here are three quick facts about Java that may interest you all!

Java is Compiled and Interpreted

Before Java was created, languages would be compiled with limited portability. For example, C/C++ would compile to binary code which would then confine it to running only on the type of operating system it was compiled on (Windows or Unix). These are known as platform-dependent languages. (Why Java is Both Compiled and Interpreted)

The introduction to Java was special because it was able to separate how a program goes from source code to output in two steps (complied then interpreted), making it a platform-independent language.

The Java compiler generates byte code, which is used by the machine to quickly interpret the program processing. When Java code is compiled, it is portable across different machines (there is not as much necessary setup for getting the application running).
The Java interpreter that runs byte code is called the “Java Virtual Machine”. It contains the ability to take the compiled byte code and convert it into readable binary code for wherever it is installed.

Java contains relational and logical operators

Relational operators are used in checking conditions like whether two values are equal, or whether one is greater than the other. All operations have an outcome of a boolean value.

Logical operators evaluate expressions using “and”, “or”, or “not” to evaluate if something is true or not. Short-Circuit logical operators include && (AND) as well as || (OR).

With &&, both sides of the operator must evaluate true to be true. The short-circuit nature of this operator is if the expression to the left of && is false, then Java won’t both continue to evaluate it because the complete expression cannot possibly be true.

String apples = "apples"; 
String oranges = "oranges"; 
if ( apples && oranges) {
    return "These apples are actually oranges"; 
}

// Which can be refactored into: 
if (apples) {
    return "These apples are truth."; 
}

With ||, at least one side of the operator must be true. If the left side of the operator is true, then the whole statement will short-circuit and evaluate as true.

// We know the first operator will be false, so Java will continue through this OR evaluation to check on the second one.
if( (4 > 7) || (3 < 5) ){
   return "Something has to measure right.";
}

Bonus! Java also has a conditional operator which is used to evaluate boolean expressions. It’s syntactic sugar to making a simplified If/else statement.

// Ugh, so many lines, all these symbols. yawn yawn yawn 
if(true){
  return "Hell yeah"; 
}
// Ternary time!
true ? "Hell yeah" : "Oh naw"; 

I always like to think of ternary operators like a call and response:

“Is this true? (emphasis on the symbolic question mark)”
Well, then I’ll do that (👈🏽 of the colon).
Ooorrrr I’m going to this (👉🏽of the colon).

Operators aren’t unique to Java, but they can vary from language to language. When using any of these operators, keep your code clear and easy to read. It’s so easy to jam a lot into an && evaluation or make triple-nested ternaries. But the next fellow who comes to read your code will have a hard time grasping what is truly happening in these statements.

Java Methods Either Return an Object or Not!

Because Java is an object-oriented statically typed languaged, everything needs to be associated with an object!
A method can be void, which means it executes the code but doesn’t return anything. A method can return a value, which is established when defining the method.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Scratch {
    public static void main(String[] args) {
        String line1 = "Roses are red, violets are blue";
        String line2 = "Cheese can stink, and so do you";
        String poem;

        PoetryMaker poetryMaker = new PoetryMaker(line1, line2);
        poem = poetryMaker.makePoem();
        System.out.println(poem + "\n");

        poem = poetryMaker.scramblePoem();
        System.out.println(poem + "\n");

        poetryMaker.clearPoem();
        poem = poetryMaker.getPoem();
        System.out.println(poem + "\n");
    }

    static class PoetryMaker {
        String lineOne;
        String lineTwo;
        String poem;
        public PoetryMaker( String lineOne, String lineTwo) {
            this.lineOne = lineOne;
            this.lineTwo = lineTwo;
        }

        public String makePoem(){
            this.poem = lineOne + "\n" + lineTwo;
            return this.poem;
        }

        public void clearPoem(){
            this.poem = "";
        }

        public String scramblePoem(){
            List<String> shufflePoem = Arrays.asList(poem.split(" "));
            Collections.shuffle(shufflePoem);
            return String.join(" ", shufflePoem);
        }

        public String getPoem(){
            return this.poem;
        }
    }
}

Here’s an example of a Poem Maker class where it receives two lines, and can set poems, scramble, and clear them. It uses examples of methods that return a value or void.

public String makePoem(){
    this.poem = lineOne + "\n" + lineTwo;
    return this.poem;
}
// Returns a String with the two likes combined
public void clearPoem(){
    this.poem = "";
}
// Clears the class's instantiation of PoemMaker's private variable of poem. Does a thing, but doesn't return. 

What are some parts of Java that you find interesting? Have you built a project using Java?

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *