Skip to content

Java specific notes

Martin edited this page Jan 19, 2018 · 17 revisions

Java as a language is really specific like any other.

Main aspects of this language are:

  • Java is compiled language
  • Java is object oriented
  • Java run in Virtual Machine, which should help with mutli-platforming
  • Java is strongly theoretically designed - specification first, implementation afterwards.
  • Java try to be open-source and "vendor independent". For many concepts, there exists different implementations and variations. Something like IE, Firefox and Chrome, they all support HTML specification but everyone use it little bit diffidently.
  • Java is mostly for commercial projects - the ecosystem around java is very old, stable and mostly focused on commercials projects.
  • Java is old and try to be stable as possible.
  • Java try to enforce good programing patterns, sometimes for cost of over complexity.
  • Java use many programming methods and technology, which should help to make programing easy. Sometimes it does the work and sometimes it's just not working - see Java Applets, Java2EE and other dead java projects.

Java programming patterns

Java is objected oriented, that's kind of different from functional programing. Everything in Java is object, except from some specific cases. Let's talk about Immutable and Not-Immutable objects.

Immutable and Not-Immutable objects

I don't know, who design java terminology but I never understand, why they use this fancy world. It's much better to replace it with different changeless or unalterable. So Immutable is changeless, unalterable. That mean, not possible to change.

What does that mean?

Object's in Java can be changeable or changeless. Object that you can not change are called changeless. After you create them, you can not change their content. Few examples of this objects are:

  • String
  • Integer
  • Boolean

This types do not have any method, which would allow you to change their value or content. They don't provide any method like "setValue()" or "updateValue()". To change value of this object, you need to create new one.

You create a new String object and store reference in variable a. Now, if you would like to change value of a you need to create new string and assign it to variable. There is no other way around. The old reference to the String will be destroyed automatically, by java mechanism called garbage collector.

// Create new String
String a = "19";

// Create another String
String a = new String("19");

// And one more
String a = String.valueOf("19");

In above example, we use different possibilities how to create String. Every time we creating new String, even if the value of the String is same as previous! That's really important. What is happenening behind the scene is something like this:

Variable a = empty;

// Create new String
String a = "19";

// Assign new reference variable a to new String
Variable a = reference #1

// Create another String
String a = new String("19");

// Old reference is replaced with new reference
Variable a = reference #2

// And one more
String a = String.valueOf("19");

// Old reference is replaced with new reference
Variable a = reference #3

// Unused references are collected and then removed by Java Garbage Collector
reference #1 - will be removed
reference #2 - will be removed

For better understanding, now see some objects, which can be changed.

Changeable objects

Object in java can look like this:

class MyObject{

    private String valueOfObject;

    public void setValue(String value){
        this.valueOfObject = value;
    }

}

Clone this wiki locally