Skip to content

Possible class names in Java

What would be the result of execution of the following code?

package com.imsavva;

public class Test {
    public static void main(String[] args) {
        System.out.println(new _成分$().getGreeting());
    }
}

class _成分$ {
    public String getGreeting() {
        return "Hello";
    }
}

If your answer is “Hello”, then you’re right!
You can use any characters from the Unicode charset, in any case, to name classes (as well as variables and methods), except some preserved words like “null”, “volatile”, “public”, etc.

However, it’s strongly recommended to stick with the Java code conventions. The rules related to the class names are on page 10.

First two reasons to use the code conventions to me are:

  • Readability – I would love to meet class names like “Eyjafjallajökull” from time to time. But, the rarer the better.
  • Convenience for programmers from different countries – imagine the situation when you have to work with the code where class names contain symbols that are missing on your keyboard? 🙈

Switch case and default

int i = 0;

switch (i) {
    default:
        System.out.println("1");
    case 1:
        System.out.println("2");
    case 2:
        System.out.println("3");
}

This snippet will compile and run without issues. It might be dubious whether it’s okay to put the “default” block at the first place, but you can place it anywhere. In this case, the result would be:

1
2
3

However there is a good practice to put the “default” block at the end of the switch statement. Or avoid using switch statements at all. 🙂

Variables and fields instantiation

package com.imsavva;

public class Test {
    private String testField;
    
    public static void main(String[] args) {
        String testVariable;
        System.out.println(testVariable);
    }
}

This code won’t compile because of line 4:

When we declare a local variable, it must be initialized before use. Class fields don’t have to be initialized. In case we didn’t specify any value, a default value will be used.

The following list represents default field values:

    private byte b;     // 0
    private short s;    // 0
    private int i;      // 0
    private long l;     // 0
    private float f;    // 0.0f
    private double d;   // 0.0
    private char c;     // '\u0000'
    private String str; // null

1Z0-808 Preparation notes

The process of studying Java Core. The picture was taken from Unsplash.com.

During the preparation time, I was making notes to remember little tricks that I didn’t know or forgot. Although I would never recommend using some of the pieces of code from the notes in production, they might be helpful for those who are preparing for the exam.

So, as I mentioned in this post, I will do my best to keep my New Year’s resolution and post the most useful notes in my blog. Along with the main topic I will try to explain why some of the code examples should never appear in your production code.

All the posts related to this topic will be marked with a tag 1Z0-808.

A review of the 2018

I think it is the first review of the past year I ever wrote, but it might become a good tradition 😊
So, below I list some highlights from the outgoing year. Let’s start from a funny pic of my cat :3

Winter 2018

I bought a new phone on January 4 and this time it was Android. It is my first Android phone, before that I used Apple. Everything is fine and I even fell more comfortable with some features iPhones don’t have. However, I think Apple makes more secure phones and I miss iPhone’s camera quality. I broke its screen somewhen in summer when I was helping my friend to move house. No photos here. 📱

Usually, I don’t like cold weather, snow and so forth, but this year I tried mountain skis and then snowboarding. Now I like winter and mountains. We spent our vacation at Sochi’s Rosa Khutor ski resort in April, participating at Quicksilver New Star Camp festival, which was fantastic.

In Spring, I participated in my first hackathon. We were developing an application in a team of 5 people for almost 24 hours without sleep! Our application was intended to track parking places on a car park and display which place is taken and which one is free on a mobile phone. On the back-end side, we used Tensor Flow to analyse a video stream from a camera, Java and PostgreSQL to handle and store the data. On the front-end side, we used React, GraphQL and PWA to fetch and display the data on a mobile phone.

Meanwhile, a Vietnamese cafe opened in my hometown. We often visit it with friends and have some good food there.

I visited many beautiful places and events.

We spent a great time in Konstantinovo near Ryazan (actually it wasn’t so great because of a ton of mosquitoes!)

We went rafting for 3 days!

We visited a sober party of free travellers and hitchhikers. It was cool to listen to people who crossed the whole Russia on suburb trains, lived in different countries for months, travelled to Africa and Sri Lanka, not just to stay in a 3, 4 or 5 stars hotels, but to live among other people from those countries. I walked away with a conclusion: if you have to walk for several hours, please, wear some comfy shoes next time!

We had a great summer party with our colleagues. We played different games, swam in a river and chilled.

In August we had our second vacation. Our friends came to visit us in Ryazan. Yes, my haircut definitely proves that I’m on vacation. 😁

Suddenly we decided to go to the seaside by car in a few days. We packed our luggage and covered about 3000 km in the next ten days. We stayed in campings, it was my first experience living at the sea in a tent. A good experience!

Are you still reading? Wow. You should know that I am already tired writing this post 😃. I think my year was really cool! So, this was the summer.
In autumn we also had two interesting trips and new experiences. We visited Yaroslavl, a beautiful city where my grandmother lived before getting married.
It was my first time staying in a hostel. It wasn’t very comfortable because of small children crying. 🙄
I’ve already written about Yaroslavl here.

About 13 years ago (omg really), I played an MMORPG called Lineage II. One of my friends invited us to visit a festival dedicated to this game. One of my favourite music bands who play power metal 🤘🏻were there.

In October, we spent a night in nature, built a banya, had a steam bath, swam in the Oka river, walked to a “drunk forrest” and had a lot of fun (we even had problems with the police, but eventually everything was fine).

To continue the topic about PC games, I visited a Dota 2 tournament in Moscow. I play it occasionally, but I’m not a professional. It was interesting to see how the teams were smashing each other to get a prize.

Huh, that’s it about traveling in 2018. I didn’t know I visited so many places! 😃

In 2018 I became modern and bought a wearable device. It’s the Xiaomi Amazfit Bip smart watch. Now I count my steps and measure the deep sleep phase. What’s the most amazing about it – I can set up a countdown timer right on my hand when I boil eggs! 🐣

I also started doing yoga and running. However, I prefer running outside and when it’s warm, so I plan to continue running in Spring 2019. Yoga is fine anytime!

I played as many tabletop games as I never played before. I think the number is more than 10!

We met really nice guys and we still keep in touch – Kostya, Dima, Anya and Nastya. They visited us in Ryazan and created a funny local meme about one of the cafes we visited. 💇🏻

I held a snake! I’m afraid of them. Actually, I can’t say it was a try to overcome one of my phobias, as it was not poisonous and dangerous. It was really cute and friendly. Meet the corn snake:

A few words about my plans that didn’t have a chance to be brought to life.

I haven’t tried hitchhiking
I haven’t had a several-days-long ride on a bicycle
I haven’t run my own pet project (but sometimes I commit there 🙂)

I hope to turn these into reality next year.

Happy New Year! 🎉🎄
2018 was very good, wasn’t it?
I wish you all the best in 2019!

Passed the Oracle 1Z0-808 certification

Good news, everyone!

Even though this is the first blog in 2018, I’m still alive and I’m happy to say that I have passed the Oracle Certified Associate exam with 94% of correct answers (I still can’t believe it 😱).

It took about a month to prepare and I was spending all my free the time after work to read a book and write and test code examples.

While preparing, I have made more than a hundred of small notes, and I hope to find time and motivation in the next two months to post them here.

See you in 2019 🙂🎄

Using StringBuilder instead of usual concatenation. When is it appropriate?

You might know that using string concatenation in Java is not a good practice as it might affect performance. In this short article, I will try to describe when it is necessary to use StringBuilder and when we can afford using concatenation (+ sign).

Let’s start with a simple example:

package com.imsavva;

public class ConcatenationTestApp {

    public static void main(String[] args) {
        String str = "abc";
        str = str + "def";
        str += "12" + "34" + str;

        System.out.println(str);
    }
}

To see how it works under the hood, let’s compile and decompile it. I tried this using Java 6 and Java 8.

Compiling the code:

javac src/com/imsavva/ConcatenationTestApp.java

Decompiling the class:

javap -c src/com/imsavva/ConcatenationTestApp

Continue reading to see what happens next.

Read More →