EMLyon logo corp

 

1. Variables and objects

General rules:

  • The type of the variable (String, Long, Boolean…​) start with a capital letter.

  • variable names start without a capital letter.

String variables store text

Creating a String variable and giving it a value
String title = "Welcome to my app"; (1)
1 Don’t forget the double quotes " "!

Integer variables store round numbers

Creating an Integer variable and giving it a value
Integer classSize = 32;
int anotherClassSize = 25; (1)
1 int is like Integer. It takes less memory but is sometimes less convenient to use. Also, note: no double quote!! Double quotes are just for String.

Float and Double variables store decimals

Double is like Float but can store decimals with a lot more precision

Creating Float and Double variables
Float pi = 3.14f; (1)
Double piVeryPrecise = 3.141592653589793238462643383279502884197169d; (2)
1 Don’t forget the f letter at the end of your number. float or Float can be used, float takes less memory than Float.
2 Don’t forget the d letter at the end of your number. double or Double can be used, double takes less memory than Double.

Long stores big round numbers

Long is like Integer but can store bigger numbers

Creating a Long variable
Long millisecondsSinceLastMonth = 364434030430414l; (1)
1 Don’t forget the l letter at the end of your number. long or Long can be used, long takes less memory than Long but can be inconvenient to use.

Boolean stores true / false values

This seems not very useful but actually we use it quite often

Creating a Boolean variable
Boolean hasAStudentCard = true; (1)
1 A classic mistake is to write "true" (with double quotes, which is incorrect). Boolean values are true or false without double quotes " ". boolean can be used instead of Boolean: less memory but also less convenient to use in some cases.

Objects: to create and store a variety of things

A variety of objects exist - use them to create and store things

Creating an Object storing a Date
Date dateStartOfTheGame;  (1)
dateStartOfTheGame = new Date(); (2)
Date dateEndOfTheGame = new Date();  (3)
1 An object dateStartOfTheGame of type Date is declared. It is null at the moment.
2 dateStartOfTheGame is instantiated: an instance of it is created.
3 Shortcut: a variable can be declared and instantiated in one line of code.