[Java notes] Very basics

Rafaela Grison
4 min readNov 10, 2022

--

These notes are assuming Java is NOT your first language!

Convention:

In java the variables are written in lowerCamelCase format. For classes, it has to be in CamelCase.

To store a variable in java, you need 3 pieces of information:

  1. The type of value.
  2. The variable name.
  3. The variable value.

Ex: int passengers = 20;

Java is Strongly Typed, which means once you define the value type of a variable you can only store values of that type. You can’t store a text in an int variable, if you do that, the code is not gonna compile.

  • Java is case sensitive. int passengers is not the same as int Passengers.
  • You can set variables equal to each other. If I set int busTickets = passengers, busTickets will have the same value as passengers (20).

How to update a value inside a variable:

* Create a new class named Bus.Java. * Make sure it has a main() method.

To update the variable, just need to set it equal to a new variable, and the value inside of it changes every time you update it.

When to use char vs String:

Char only stores 1 variable, and it needs to be wrapped by single quotes.

If String is more flexible and can also store single characters, why not always use String type?

Because of memory usage and performance. Char takes up less memory and is faster, because char is a primitive type whereas String is an object.

When to use int vs long:

Int can ‘only’ store numbers inside 2 billion.

When using long, we need to add an L at the end of the number to reassure Java that you’re using a long to store big numbers. (ex: long population = 700000000L;)

For the same reason that we use char when we need to store only 1 character, using long to store small numbers is inefficient because it takes up more memory and slows down the performance.

Double type:

Can be used to store decimals, while int and long can’t. When you store a number in double, even if you don’t specify it as a decimal, double will store it as one. So if you say double num = 10, it is stored as 10.0.

Double should always be used for math calculations.

Can use type casting to convert a type for another:

Ex. to convert a double value to int:

If we have a double decimal = 10.5 and we want the int integer = to decimal, we need to first specify the type we’re casting to and the original value, like: int integer = (int)decimal; So java will cut the decimal number and store only 10 in the variable integer.

Never use == or != to compare string values, to compare strings, use: sentence.equals(sentence2) or !sentence.equals(sentence2)

Java uses the || (OR) and && (AND) as comparison operators.

When to use switch instead of else if:

Switch compares one value against a list of values. It compares an argument with a list of cases, if the argument matches the case1, case1 runs.Don’t forget to add a break; after the case so Java knows it needs to stop running after it matches the case.

  • 95% of the time, you will use if-else.
  • If-else can test any condition, while switch can only compare one value against a list of other values.

print vs. println:

println() prints text and moves to a new line.

System.out. println ( “ a “ ) ;

System.out. println ( “ b “ ) ;

System.out. println ( “ c “ ) ;

>> a

>> b

>> c

print prints text but it does not move to a new line. So, ensuing print calls print on the same line.

System.out. print ( “ a “ ) ;

System.out. print ( “ b “ ) ;

System.out. print ( “ c “ ) ;

>> a b c

--

--

Rafaela Grison

From business to SWE. I’m writing as I’m learning, welcome to my journey!