There are 2 types of variables:
Boolean & char
Type | Bit depth | Value Range |
---|---|---|
boolean | JVM Specific | true/false |
char | 16 bit | 0 to 65535 |
Integer
Type | Bit depth | Value Range |
---|---|---|
byte | 8 bits | -128 to 127 |
short | 16 bit | 32768 to 32767 |
int | 32 bits | -2147483648 to 2147483647 |
long | 64 bits |
Floating point
Type | Bit depth |
---|---|
float | 32 bits |
double | 64 bit |
public static void main(String[] args){
int i = 25;
System.out.println(i);
}
public static void main(String[] args){
int i = 25;
byte b = i;
System.out.println(i);
}
public static void main(String[] args) {
int i = 25;
byte b = (byte) i;
System.out.println(b);
}
int i = 128;
byte b = (byte)i;
public static void main(String[] args) {
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;
long l = 1234L;
double d = 123.4;
float f = 123.4f;
System.out.println (result);
System.out.println(f);
System.out.println(l);
System.out.println(capitalC);
}
public static void main(String[] args) {
int i = 23;
int j = 5;
int sum = i + j;
int difference = i - j;
int quotient = i / j;
int remainder = i % j ;
int product = i * j;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
System.out.println("Product: " + product);
}
public static void main(String[] args) {
double i = 21.7;
int j = 5;
double sum = i + j;
double difference = i - j;
double quotient = i / j;
double product = i * j;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Quotient: " + quotient);
System.out.println("Product: " + product);
}
Data types (primitives):
https://www.hackerrank.com/challenges/java-datatypes
https://www.hackerrank.com/challenges/30-data-types
(day 1)
Note: You need to register for 3- days of code and another challenge unlocks every day (for 30 days).
Useful to go with the primitives:
https://www.hackerrank.com/challenges/30-operators (day 2)
A bit more challenging:
https://www.hackerrank.com/challenges/java-int-to-string
public static void main(String[] args) {
int hourOfTheDay = 10;
if (hourOfTheDay < 11) {
System.out.println("Good morning!");
}
}
public static void main(String[] args) {
int hourOfTheDay = 17;
if (hourOfTheDay < 11) {
System.out.println("Good morning!");
} else {
System.out.println("Good afternoon!");
}
}
public static void main(String [] args){
int hourOfTheDay = 17;
if (hourOfTheDay < 11) {
System.out.println("Good morning!");
} else if (hourOfTheDay < 15) {
System.out.println("Good afternoon!");
} else {
System.out.println("Good evening!");
}
}
public static void main(String [] args){
for (int x = 0; x < 10; x = x + 1) {
System.out.println("x is now " + x);
}
}
public static void main(String[] args) {
int x = 15;
while (x > 12){
System.out.println(x);
x = x - 1;
}
System.out.println("Last value: " + x);
}
public static void main(String[] args) {
int x = 15;
do {
System.out.println(x);
x = x - 1;
} while (x > 12);
System.out.println("Last value: " + x);
}
public static void main(String[] args) {
int j = 5;
for (int i = 0; i < 10; i++){
if (i == j) {
System.out.println("I found it!");
} else {
System.out.println(i);
}
}
}
public static void main(String[] args) {
int j = 5;
for (int i = 0; i < 10; i++){
if (i == j) {
System.out.println("I found it!");
break;
} else {
System.out.println(i);
}
}
}
https://www.hackerrank.com/challenges/java-loops-i (simple for loop)
https://www.hackerrank.com/challenges/java-if-else
https://www.hackerrank.com/challenges/30-conditional-statements (day 3 - if/else)
https://www.hackerrank.com/challenges/30-loops (day 5)
https://open.kattis.com/problems/timeloop (for loop)
Create a new class
public class Dog {
//field
String name;
//methods
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void bark(){
System.out.println(name + ": wuff! wuff!");
}
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.setName("Benny");
System.out.println("The dog's name is: " + dog.getName());
dog.bark();
}
Add the following code in the main method:
Dog dog = new Dog();