Back to Top
 
 

Why Java?

The programming language known today as Java has been in development since 1990. James Gosling - considered to be the language creator - first called Oak to commemorate the tree he was seeing from his office window. It soon turned out that the name was in use and needed to be changed.

This time inspiration struck him in a coffee shop: Java is a variety of Arabica coffee.

Why Java? The answer is simple - Java is everywhere! Is there anyone who has not yet heard about it? Versatility was the creators' goal: the programs were to be portable and hardware-independent. And this was the key to success.

Nowadays Java is used across web applications, desktop projects, and Android-based mobile phone apps.

Java – an object-oriented language

What is the most characteristic feature of Java? A spiteful answer could be: the pervasive prevalence of objects. In a way it's true: objects are first-class citizens in Java. We assume that everything is an object: the world is built from them and each object has certain features and a state. Look around: what objects do you see? A window? Flower? Fish? What are their attributes (color, shape)? How do they behave (move, grow)?

Types and variables

We will store particular elements in variables. A variable can be imagined as a box with something inside. One box only has enough room for one element.

Java is a strongly typed language: when we put something in a box we need to at once declare what type it is. It's also important to remember that the type, once it is assigned, it can never change. The value of the variable has to match the declared type: otherwise the program will have an error.

Not all languages are so restrictive about typing; Java is uncompromising and catches every little inaccuracy. Thanks to it we can be sure that no unexpected conversions happen in the program.

We can differentiate three general data types:

  • string (text type: values are given in quotes),
  • int (numeric type: integer numbers from -2 147 483 648 to 2 147 483 647),
  • boolean (logical values: True or False).

We already know what a variable is and that it must have a type. Now let's learn how to declare variables: that is, how to assign values to them.

We can represent it like this:
variable_type variable name;
variable_name = variable_value.

There's a shorthand for this (it works the same):
variable_type variable_name = variable_value;

Knowing that we need variables and that they need to have a proper type, let's try to describe a dog.

See examples of types and variables:

How to name variables?

The best name explains what the variable stores. We may remember for a while that the "abc" variable stores the number of parts used to repair a car. But if someone else sees the code they will have no idea what it means. That's why it is best for a box to have a label corresponding to what's inside it. Of course, it is up to the programmer to decide how to name the variables.

But there are some rules everyone needs to follow:

  • name of a variable starts with a lowercase letter, a dollar or underscore character;
  • name of a variable starts with a lowercase letter, a dollar or underscore character;
  • character case matters: variable 'abc' is not the same as 'ABC';
  • if the name has more than one word we separate them with underscores or start each of them with an uppercase letter.

Displaying data

Let's try and show the data on the screen. Once again let's use the structure:

System.out.println();

We can show the values of variables on the screen using their names directly.

 

Here's an example:

System.out.println(“The dog is called ” + name); // prints: “The dog is called Buddy”.

Note: if you want to enter the text to display, put it inside quotation marks; if you are referring to a variable, just write its name. If you want to connect them, you can do it using the plus "+" character.

See an example of displaying data in Java:

Comments

If you want to describe how the code works you can use comments:

  • single-line comment is preceded by two slashes
    (example: // this is a single-line comment)
  • multi-line comment is surrounded by slash and asterisk characters
    (example: /* this comment
    can span
    several lines */).
 

Login instructions

Before you start working on the tasks go to: https://repl.it/ and create an account by clicking the "Sign Up" button in the upper right corner. Next, to verify your account, follow the instructions from the page.

Done? Great! Now you can start doing the tasks.

Remember to click the "Fork" button when you go to the page with each task. This will let you change the code.

 

Tasks

 

Task 1

Before you read and run your first program remember that some structures are going to be unclear for you. Do not worry about it, at the beginning you don't need to know them and everything is going to get clearer as you learn the language better.

Go to the first task:

Use the Run button to start the program.

We are mostly interested in the line: System.out.println(“Hello World”). As you can see, it has the string that gets displayed after running the program.

Now try to replace "World" with your name. Run the program again and check the result on the screen.

Look at the code again: apart from the section that gets executed, there's also a comment describing what is happening in the code. The comment is passed over during program execution. With simple programs it may seem unnecessary but in more complex projects it may turn out crucial, especially when we need to come back to a program after a while and change it or use it again. Comments are also appreciated by other programmers who want to use our code.

Task 2

According to the pattern and best practices, declare a few more variables with different types and assign values to them. You can describe your pet (a cat, rabbit or tortoise: its age, weight, the sound it makes, etc.).

Go to the second task:

Try to assign a number to a String type variable, and the other way round: a text value to a numeric variable. This will of course not work, but notice the error information.

Task 3

Copy all the variables describing your pet from the file in Task 2 and paste them in the indicated spot in the program.

Display the values of all declared variables. You can do it by copying the System.out.println(); method for each variable or print them one by one connecting them with the „+”character.

Go to the third task:

Task 4

The variable car has been declared in the code. Enter three attributes of type String, int and boolean each. Display them on the screen descriptively (linking the text to the variable).

Go to fourth task:

Task 5

Declare variables of type String that will contain the names of your family members. For each of them add attributes:

  • kinship,
  • age,
  • eye color,
  • hobby, etc.

Try to create a nice description of each of these people, incorporating the variables properly.

Go to the fifth task: