DEV Community

Dev From Legacy
Dev From Legacy

Posted on

Java first steps: 20 things I'd forgotten

If you don't work with Java or you're relearning it (like me!), I'm sharing some things:

1. Java Major features: Object Oriented, Platform independent, it has many libraries and it's similar to C++.

2. Java Virtual Machine(JVM): Java source code [.java] is compiled into Java bytecode [.class], which is executed by a Java virtual machine (JVM).

3. Important Versions: 1 [1995], 5 [2004] and 8 [2014]. Current version in 2020-09: 9.

4. Java Kit Development: it includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in Java development.

5. Environment Variables: unless on Windows, you will need to set environment variable.

6. Compiling/Executing Java on Command Prompt: you can compile your .java program using javac MyProgram.java and you can run using java MyProgram.

7. Best options IDE for Java: currently Eclipse, Netbeans and IntelliJ IDEA.

8. Java Hello World: in the Java programming language, every application must contain a main method whose signature is: public static void main(String[] args).

class HelloWorldProgram {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

9. Java Comments: Single-line comments start with two forward slashes // and multi-line comments start with /* and ends with */

10. Java naming convention - CamelCase: Java follows camel-case syntax for naming the class, interface, method and variable. Method and variable should start with lowercase letter (example: ImageSprite ). Class and interface should start with uppercase letter (example: getBackground ).

*11. Java Operators: *

  • Arithmetic [+, -, *, /, %, ++, --]
  • Logical [&&, ||, !]
  • Comparison [==, !=, >, <, >=, <=, >>=, <<=]
  • Assignment [=, +=, -=, *=, /=, %=, &=, |=, ^=]

12. Concatenation by +: using operator + is one way to build a string in Java.

public class TestVariables {

    public static void main(String args[]) {

        int age = 30;
        System.out.println("Age: " + age);
    }
}

13. Floating-Point Data Types: Java has some peculiarities about floating-point, like the outputs below.

public class TestFloatingPoint {

    public static void main(String args[]) {

        double salary = 1250.70;
        System.out.println("S: " + salary); //Output: S: 1250.7
        double division = 3.14 / 2;
        System.out.println(division); //Output: 1.57
        int otherDivision = 5 / 2;
        System.out.println(otherDivision); //Output: 2
        double newDivision = 5 / 2;
        System.out.println(newDivision); //Output: 2.0
        double lastDivision = 5.0 / 2;
        System.out.println(lastDivision); //Output: 2.5
        double number1 = 0.2;
        double number2 = 0.1;
        System.out.println(number1 +number2);
        //Output: 0.30000000000000004
    }
}

14. Java Type Casting/Conversion: Java Type Casting is classified into two types: Explicit and Implicit.

public class TestConversion {

    public static void main(String[] args) {

        //Implicit casting
        byte a = 40;
        short b = a;
        int c = b;
        long d = c;
        float e = d;
        double f = e;
        System.out.println("byte : "+a); //Output: 40 
        System.out.println("short : "+b); //Output: 40
        System.out.println("int: "+c); //Output: 40
        System.out.println("long: "+d); //Output: 40
        System.out.println("float: "+e); //Output: 40.0
        System.out.println("double: "+f); //Output: 40.0

        //Explicit casting
        double g = 30.0;
        float h = (float) g;
        long i = (long) h;
        int j = (int) i;
        short k = (short) j;
        byte l = (byte) k;
        System.out.println("double: "+g); //Output: 30.0
        System.out.println("float: "+h); //Output: 30.0
        System.out.println("long: "+i); //Output: 30
        System.out.println("int: "+j); //Output: 30
        System.out.println("short: "+k); //Output: 30
        System.out.println("byte: "+l); //Output: 30
    }
}

15. Characters and Strings in Java: you can see some examples bellow.

public class TestCharacters {

    public static void main(String args[]) {
        char letter = 'a';
        System.out.println(letter); //Output: a
        char letterValue = 65;
        System.out.println(letterValue); //Output: A
        char nextLetterValue = (char) (letterValue + 1);
        System.out.println(nextLetterValue); //Output: B
        String sentence = "String test";
        System.out.println(sentence); //Output: String test
        String newSentence = "String test" + 1;
        System.out.println(newSentence); //Output: String test1
    }
}

16. Conditionals in Java: you can see some examples bellow.

public class TestConditional {

    public static void main(String[] args) {

        int age = 22;
        int numberPeople = 1;
        boolean accompanied = (numberPeople > 1);
        if ((age >= 21 || accompanied) && numberPeople < 20) {
            System.out.println("You are allowed!");
            switch (numberPeople) {
                case 1:
                    System.out.println("Table for 1 on the right!");
                    break;
                case 2:
                    System.out.println("Table for 2 on the left!");
                    break;
                default:
                    System.out.println("Table for group on the patio!");
            }
        } else {
            System.out.println("You arent't allowed!");
        }
        // Output: You are allowed!
        // Table for 1 on the right!
    }
}

17. Scope: A block of code refers to all of the code between curly braces {}.

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

        { // This is a block
            int x = 100;
            // Code here CAN use x
            System.out.println(x);

        } // The block ends here
        // Code here CANNOT use x
    }
}

18. Loops in Java: you can see some examples bellow.

public class TestLoop {

    public static void main(String[] args) {
        int counter = 0;
        while(counter <= 1) {
            System.out.println(counter);
            counter++;
        }
        System.out.println(counter);

        for(int newCounter = 0; newCounter <= 1; newCounter++) {
            System.out.println(newCounter);
        }
        //Output: 0 1 2 0 1
    }
}

19. Break/continue:

public class TestBreakContinue {

    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            if (i == 2) {
                break;
            }
            System.out.println(i);
        }
        // Output: 0 1

        for (int i = 0; i < 4; i++) {
            if (i == 2) {
                continue;
            }
            System.out.println(i);
        }
        // Output: 0 1 3
    }
}

20. Arrays in Java: you can see some examples bellow.

public class TestArrays {

    public static void main(String[] args) {

        String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
        System.out.println(cars[0]);
        // Outputs Volvo        
    }
}

Top comments (2)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise • Edited

You have stated:

  1. Important Versions: 1 [1995], 5 [2004] and 8 [2014]. Current version in 2020-09: 9.

The current version of Java is JDK 15 ... released on the 15. September... Java 9 is already old... see en.wikipedia.org/wiki/Java_version... If I correctly understand that setence.

  1. Environment Variables: unless on Windows, you will need to set environment variable.

Where is the difference? On Windows you can set and should set environment variables (usually JAVA_HOME) as well as on other platform or not using environment variables at all as on other platform...

Comparison [==, !=, >, <, >=, <=, >>=, <<=]

Hm.. >>= If you check this page: docs.oracle.com/javase/tutorial/ja...
you will see the >>= belongs to the assignment operators and has nothing to do with comparison. Also for <<= which you have missed.

  1. Floating-Point Data Types: Java has some peculiarities about floating-point, like the outputs below.

You have stated there are some peculiarities.. let us take a look.

double salary = 1250.70;
System.out.println("S: " + salary); //Output: S: 1250.7

This is an assignment of a floating point (double precision) to a variable of the type double. Is this something unusual?

double division = 3.14 / 2;
System.out.println(division); //Output: 1.57

I don't see anything unusual here?

int otherDivision = 5 / 2;
System.out.println(otherDivision); //Output: 2

Integer division?

double newDivision = 5 / 2;
System.out.println(newDivision); //Output: 2.0
double lastDivision = 5.0 / 2;
System.out.println(lastDivision); //Output: 2.5

So the first one divides integer (the digit do not have .).. which result into integer division whereas the second one based on the writing 5.0 is a floating point which is by default a double see details docs.oracle.com/javase/tutorial/ja... furthermore docs.oracle.com/javase/specs/jls/s...

double number1 = 0.2;
double number2 = 0.1;
System.out.println(number1 +number2);
//Output: 0.30000000000000004

This is the usual behaviour for IEEE 754 double precision number cause internally is based on the power of 2 which means not every number can be converted from decimal into it's internal value without loss. For example 0.2 can't be expressed by a limited number of powers of 2 neither 0.1 (Take a look here: h-schmidt.net/FloatConverter/IEEE7...) this is general problem with floating point. The same result will be printed if you try the same in Rust:

let number1 = 0.2;
let number2 = 0.1;
let result = number1 + number2;
println!("{}", result);

Apart from those inaccuracy great article. Continue working on it.

Collapse
 
ender_minyard profile image
ender minyard

There's not a lot of people writing about Java. Thanks for your contribution!