Back to all lessons


Lesson 1

Hello world, bits operations

Getting started with Java programming

Task 0

This is a starter code (Hello.java):


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

Hello world!

Task 1

Write a program, that displays your name.

Task 2

Write a program, that displays a sum of two numbers. Try also: multiplication (*), substracting (-) and dividing (/).

Does dividing 11 per 2 give correct answer?

Task 3

Find binary representation for numbers: 81, 65.

Calculate value of: 81&16, 65&16.

Task 4

Check your Task 3 writing java code.


public class Hello {
    public static void main(String[] args) {
        // "0b" means "binary"
        System.out.println(0b10101);
        // some expressions
        System.out.println(0b10101 & 10);
        System.out.println(5 | 4);
    }
}
Task 5

Instead of writing the same value all the time, you can use a variable, i.e. byte.

Create a new byte variable. Next write program, that extracts powers of 2 (1,2,4,8,16,...) from the byte. i.e.: 81=64+0+16+0+0+0+1


public class Hello {
    public static void main(String[] args) {
        // create a byte called "x"
        byte x = 81;
        // display a byte
        System.out.println("x=" + x);
        // some expressions with variable "x"
        System.out.println(x & 1);
        System.out.println(x | 4);
    }
}