days
1
4
hours
0
8
minutes
4
1
seconds
3
9
search
Java 9 & Java EE 8: They are finally here!
The basics of Java

Cheat sheet: Java pointers for beginners

Martin Patsov

© Shutterstock / Rawpixel.com

Ever feel lost while programming? Not to worry! Today, Martin Patsov is here to show you there’s nothing to fear from Java.

We all start out as beginners. Today, I am going to explain the basics of Java, from the ecosystem to its execution and more. We’ll spend some time exploring Hello, World and the command line, Finally, we’ll dive in deep with some of the common mistakes made by Java beginners and how to code our way out of those holes. Let’s get started!

Main Java infrastructural components

The Java Development Kit (JDK) consists of the following components: Java Runtime Environment, Tools & Tool APIs and the Java Language. The Java Runtime Environment itself consists of Java Virtual Machine, lang and util Base Libraries, Other Base Libraries. Also, we have Integration Libraries, User Interface Toolkits and Deployment. All of this without the Deployment represents the Java SE API.

The Architecture of the HotSpot JVM is being represented from the Class Loader Subsystem, the Runtime Data Areas and the Execution Engine. We also have Native Method Libraries.

Execution of a Java Program Flow

How exactly does a computer understand code? It’s a fairly standard order of operations.

  1. The command line arguments are parsed
  2. The required memory is allocated
  3. The environment variables are loaded
  4. The class with the main method to be executed is located
  5. Creation and initialization of the VM
  6. The class with the main method is loaded
  7. The main method is executed
  8. After the execution has finished, the result is returned
  9. The VM is destructed

Hello World example

First, you need to add the following text in a file with the name PatsovHelloWorldExample.java:

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

Depending on your preference you may choose to do so with an IDE or with a text editor. If you have used an IDE, the next step is just to run the program. If you have created the file with a text editor, you need to compile the file with “ javac PatsovHelloWorldExample.java ” in the terminal and run in with “ java PatsovHelloWorldExample ” again in the terminal. And in image 2 we can see the expected result.

Hello World example

Command-line options

While starting the JVM we are able to pass some additional arguments in order to control the

execution of the application and some of the environment variables. The syntax is the following:“java [here we add some options] -jar filToBeExecuted.jar [here we pass some arguments to our application]

“java [here we add some options] -jar filToBeExecuted.jar [here we pass some arguments to our application]

An actual example with real parameters would be: “java -classpath MyApplication.jar the.package.ClassWithMainMethod”

Some of the options are “server”, “client”, “classpath” and “help”. In the arguments, we can pass whatever we need, depending on how we have developed our application and what we want to achieve.

The primitive types

Here we have the byte, represented by an 8-bit integer, with values between [-128, 127], also the short data type with 16-bit integer and respectively values between [-32 768, 32 767]. We have the int data type which is a 32 bit integer with values between [-2 31 , 2 31 -1], the long which is a 64 bit integer along with the floating point float and double, where the float is a single-precision 32-bit IEEE 754 floating point and the double which is a double-precision 64-bit IEEE 754 floating point. We have also the boolean which is either true or false and the char which is a single 16-bit Unicode character.

For currencies, you will need to use the java.math.BigDecimal class and for the really big integers you will need the java.math.BigInteger class.

The collections

All collections actually represent a container, which is grouping some logically connected components in a single whole. They are generally separated in 3 different types: for general purpose (for example the ArrayList, LinkedList, TreeMap, TreeSet, HashMap and HashSet), the concurrent ones (like the ConcurrentMap, ArrayBlockingQueue, Vector, LinkedBlockingQueue and PriorityBlockingQueue) and specialized ones (as the EnumSet, CopyOnWriteArraySet and WeakHashMap).

The collections are best for:

  • Sorting
  • Searching
  • Shuffling
  • Manipulating – reverse, fill, copy, swap, bulk add
  • Finding max/min values

Regular expressions

For best learning results is recommended solving of RegEx crosswords. Some of the most famous pattern matching expressions are:

  • ^ – for matching beginning of line,
  • $ – for matching the end of a line,
  • . – for matching any character except the new line
  • [somecharactershere] – for specifying a group of characters, from which only a single one is enough to be matched,
  • [^asd] – for matching any character not in the listed ones (exclusion),
  • * – for matching 0 or more occurrences of the expression before that (ex. asd* matches ‘as’ and ‘asd’),
  • + – matches 1 or more occurrences of the expression before that,
  • ? – matches 0 or 1 occurrences of the expression before that

Common problems and how to avoid them

Problem: Null pointer exception – this kind of exception occurs when we are trying to use null when we actually need an object
Solution: If it is possible at some point your object to become null, you should think of adding null checks to your code, especially when you did not create the object yourself. Also, it is better to return empty collections instead of nulls. Adding validations to set and get methods is also a good idea.

Problem: Confusing the comparison operator ( == ) with the assignment operator ( = ).
Solution: You will need some time to adapt if you are coming from another language. An easy trick I have used for remembering this is: if you know that you are interested in only one of the values (you are assigning the one to the other), you use single “ = ”, otherwise if you care about both variables and values (you are comparing them) you need the double “ == ”

Problem: Using comparison operator ( == ) instead of the .equals() for checking equal strings.
Solution: All Java objects are not being checked for equality with the comparison operator ( == ). Instead we use .equals() method. In Java, the String is an immutable object instead of a primitive type.

Problem: Not properly using access modifiers (private, protected, public and default)
Solution: Try to think in a more object-oriented way. Think a little more before implementing. “Should I be able to call the method from outside the class? Do I have the right to do so?” Learn more about information hiding, loose coupling and creating layers of your application.

Problem: Trying to reinvent the wheel, by implementing already existing libraries and tools.
Solution: Learn the Java standard library. Do more pair programming with other programmers. Read the section with all of the recommended Java tools in this article. Try to find tools by yourself. Learn to use Google and Stack Overflow.

Problem: Changing an object’s state through a reference to the object. This usually happens when a get method returns a mutable object. This is very unsafe!
Solution: Learn to use access modifiers properly. In the get methods return immutable objects. When creating a get method always think if it is possible to be realized an undesirable change in the state of the object after returning it. Learn about how objects are located and referenced in the memory in Java. Don’t get confused over passing by value, and passing by reference!

Problem: Syntax errors, misspellings, using wrong overloaded functions, spending too much time for clicking on the menus in the IDE in order to achieve what you want.
Solution: Get a decent IDE. The syntax errors will be detected earlier and you will be able to fix them. Learn the functionalities of the IDE. Get to know the most common shortcuts in order to save time while developing. Format your code correctly. Do not change your IDE often, or if you can at all. Learn it by heart. It will become your best self-extension. Use meaningful variable names.

Problem: Resources are not being freed. You open some connection / file / resource and not closing it afterwards.
Solution: Here comes the “Try-with-resources” statement for any class which implements the java.lang.Autocloseable interface. After the execution of the try block all opened resources in the try() part are being automatically closed. Example below:

public class TryWithResources {
   public static void main(String[] args) {
      try (BufferedReader br =
         new BufferedReader(
            new FileReader(
               System. getProperty ( "user.home" ) +
                  "/Downloads/text.txt" ))) {
         System. out .println(br.readLine());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Problem: Concatenating strings in a loop. Don’t try this at home. Permitted only under parental advisory. On each string concatenation a new String object gets created (strings are immutable). At some point, you will run out of memory or it will start taking too much time to execute.
Solution: Use StringBuilder instead of String when concatenating content in loops.

Problem: Executing SQL queries in a loop.
Solution: Optimize queries to the database as much as possible as the execution of queries is quite an expensive operation. Try to get all of the necessary information with a single query and iterate over the results.

Problem: Empty catch blocks / ignoring exceptions
Solution: The least you can do is to print the stack trace of the exception to the default output. Usually, you can use some loggers as Log4j and make the logging of information easier.

Problem: Calling run() instead of start() for starting a thread.
Solution: It is wrong to call run() directly. It is a very common bug. Always use start()! Also it is recommended to implement the Runnable interface instead of inheriting the Thread class.

Problem: Implementing without thinking enough. Some of the results can be: Deadlock, Calling non-static methods from a static context, removing items in an incorrect way while iterating a collection and all kind of logical errors.
Solution: Think. Design. Observe. If you have an hour to solve the problem, spend 50 minutes considering the solution and 10 minutes in the actual execution.

Recommendations

There are a lot of resources available online for beginners and experts alike. While these are some of my favorites, there are many more libraries, tools and frameworks available. These are my recommendations to help anyone get started:

  • Java SE – 8 [1]
  • Spring [2]
  • IntelliJ IDEA [3]
  • Angular [4]
  • Tomcat [5]
  • Maven [6]
  • Git [7]

References

[1] www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

[2] https://spring.io

[3] https://www.jetbrains.com/idea/

[4] https://angular.io

[5] tomcat.apache.org

[6] https://git-scm.com

 

To see more cheat sheets, download the latest issue of JAX Magazine:

Cheat sheets are a godsend when you don’t know something – and that will happen no matter how well-prepared you are. Programming is not a “who does better” contest and it’s just as hard as learning a new language: it takes time until you get the hang of it and master all its facets (syntax, semantics and pragmatics). It might get intimidating at times, which is why the following cheat sheets might come in handy: Java, JavaScript, Scala, Kotlin, and Dart.

And since we’re here to learn more about programming languages, why not dive deeper into the JVM universe? It will be fun, I promise.

Author

Martin Patsov

Martin Patsov is a Full Stack Developer at Dreamix, a bespoke software development company. He has experience in software development with Java, entrepreneurship, management of small teams and communication with clients. His drive is in sharing the knowledge, being the change in society he wants to see and transferring the sparkles he ignites to those around him.


Comments
comments powered by Disqus