18 March 2022

Java

Test Your Java

Step 1 Create this program Test.javain VSCode.


public class Test
{
}

If VSCode suggests installation of Java extensions, let it install them.

Step 2 Compile your code. You can do this at the command line by typing javac Test.java. If this is successful, you will see a new file, Test.class in the same folder as your Java file.

Step 3 Modify Test.java like so.


public class Test
{
    public static void main(String[] args)
    {
        System.out.println("Test Success!");
    }
}

Step 3 Recompile Test.java with javac. The triangle may both compile and run the program. If it does not run, type java Test in the command line, it your program should output Test success! .

What the heck is all of this mishegaas?

Java has two steps to executing a program. First you must compile the program. The compiler checks to see if your program makes grammatical sense. If it does not, the process aborts. You must edit your code to fix the grammatical problems. Java will isssue forth with error messages that will help you solve the problem. The name of the compiler is javac. To compile the program Test.java, you type javac Test.java at the command line.

If compilation succeeds, you wil get a .class file that contains java byte code. To be executale, your program needs a main method which looks like this.


    public static void main(String[] args) 
    {
    }

If you are using VSCode, type psvm and the main method will automatically be generated for you.

Put a main method in your program Test.java. To run it, type java Test at the command line. If you hit the triangle in VSCode, it compiles and runs all at once.

There are diagrams on my office door for the life cycles of Python and Java programs. Come by and take a picture.

Output Routines

This Java program puts "Test successful!" to the screen.


public class Test 
{
    public static void main(String[] args) 
    {
        System.out.println("Test successful!");
    }
}

Notice that all worker statements end in a semicolon (;). Omitting one causes a compiler error.

A Little About Variables

Java has an integer type. Here we see it in action.


public class Test 
{
    public static void main(String[] args) 
    {
        int x = 4;
        //try this and get rebuffed:  x = "cats";
        String s  = "cowabunga";
        System.out.println(x);
    }
}

The variable x has type int. The type of a variable is an immutable property of a variable. Only integers may be assigned to x. If you try to assign a string to x on the next line, you wil get a compile-time error. We also see a variable s of type String.

Java is a statically compiled lanaguage: all types must be known at compile time.

Java has two types of comments. The // token is a one-line comment just like Python's #. The other is /* */, which can span many lines.

To get at characters in a string, you cannot use []. You must use the charAt method as shown here. Indexing in Java and Python work identically.


public class Test 
{
    public static void main(String[] args) 
    {
        int x = 4;
        String s  = "cowabunga";
        System.out.println(x);
        System.out.println(s);
        System.out.println(s.charAt(0));

    }
}

Try printing out more characters from the string.

Boola, boola! Java has a boolean type with values true and false.,/p>


public class Test 
{
    public static void main(String[] args) 
    {
        int x = 4;
        String s  = "cowabunga";
        System.out.println(x);
        System.out.println(s);
        System.out.println(s.charAt(0));
        System.out.println(2 + 2 == 4);  //boolean: true/false
    }
}

Java has Math. Look in the Java Math API and see all of the cool stuff inside!



public class Test 
{
    public static void main(String[] args) 
    {
        int x = 4;
        String s  = "cowabunga";
        System.out.println(x);
        System.out.println(s);
        System.out.println(s.charAt(0));
        System.out.println("Test successful!");
        System.out.println(2 + 2 == 4);  //boolean: true/false
        double iq = Math.sqrt(2);//same as Python's floating-point number
        System.out.println(iq);
        System.out.println(Math.cos(0));
        System.out.println(Math.pow(5,3));
    }
}

There is no ** in Java; try it and see the ire of the compiler.

Finally, some string and formatting tricks. Want to know more? See the Java String API.


public class Test 
{
    public static void main(String[] args) 
    {
        int x = 4;
        String s  = "cowabunga";
        System.out.println(x);
        System.out.println(s);
        System.out.println(s.charAt(0));
        System.out.println("Test successful!");
        System.out.println(2 + 2 == 4);  //boolean: true/false
        double iq = Math.sqrt(2);//same as Python's floating-point number
        System.out.println(iq);
        System.out.println(Math.cos(0));
        System.out.println(Math.pow(5,3));

        System.out.print("print");
        System.out.println("me");
        System.out.printf("%s + %s = %s\n", 3, 4, 3 + 4);
        System.out.println("some" + "thing");//concatenates
        System.out.println("some" + 5);//concatenates
    }
}