How to code with Java?

how to code with javaJava is an object-oriented programming language. Applications run inside the Java Virtual Machine (JVM). JVM is a software that can be installed on every Operation System, which means that one java app does not depend on the environment (“Write once, run anywhere” principle). How to code with Java? Easily!

Java is a strong tool used in server-side enterprise solutions. It allows to create robust and reliable applications in realms of banking, billing, economics, analytics, commerce, etc.
Java has a Garbage Collector that manages memory automatically. You can use any of out-of-box data structures (Lists, Maps, Sets, Queues in many implementation).

To use this How to code with Java tutorial you need to install java environment. Use this guide.

First program

Let’s create and run a simple program that prints the sum of 2 specified numbers. We will set this numbers as command line parameters.

public class Sum {
    public static void main(String[] args) {
        int firstNumber = Integer.parseInt(args[0]);
        int secondNumber = Integer.parseInt(args[1]);
        System.out.println("Sum is: " + (firstNumber + secondNumber));
    }
}

Then let’s save this document as Sum.java.

Next open folder with this file in console and compile this class:

javac Sum.java

Now you can see file Sum.class in the same folder.

CLASSPATH

Java uses a CLASSPATH. This is just a environment variable with specified locations of Java classes on computer. You should add a folder with compiled class to this variable. Then you’ll be able to run your code.

java Sum 34 11

34 and 11 are the parameters that we want to add in our first program. They will appear inside args array of the main(String[] args) method (checkout the main method below).

Statically typed

In java you should set the type when declaring a variable or declaring return type of a method:

String s = “My string variable”;
int age = 24;

 

void printSum(int firstNumber, int secondNumber) {
     System.out.print(“Sum is: ” + firstNumber + secondNumber);
}

Main method

Java has to know where is the beginning of your program. It’s always a static method “main”:

public static void main(String[] args){
    // your code here
}

Encapsulation

Java provides encapsulation mechanism with keywords.

public – field or method is accessible for classes that use or inherit this class

protected – field or method is accessible for classes that only inherit this class

private – field or method is inaccessible from outside of the class (only visible in scope of the class that declares them)

If none of such keyword were specified, the default “package-private” restriction will occur. It will make its target accessible for classes of the same package only.

Conclusion

Java has a great amount of interesting features that are not described in this How to code with Java guide. You can discover more information that will show you the real power of this programming language. Java made a great impact on high-load and Big Data perspective. The Hadoop framework, Cassandra database, some server-side parts of your favorite social networks are also written in Java.

Leave a Reply

Be the First to Comment!