REMEMBER JAVA

MINTE9
2REMEMBER




Numbers

! There where times when we need to use objects insteed of primitives. Java provides wrapper classes for each primitive data types.
 RunCode
class LearningApp {

    public static void main(String[] args) {
        
        /** 
         * All wrapper classes are subclasses of ...
         * the abstract class Number
         */
        Number no = new Float("22.33");
        
        System.out.println(no.shortValue()); // 22
    }
}
... 10 lines
˄˄˄

Autoboxing

! Autoboxing is the automatic conversion that Java compiler makes between the primites and the corresponding object wrapper classes.
 RunCode
import java.util.List;
import java.util.ArrayList;

class LearningApp {

    public static void main(String[] args) {
        
        List<Integer> li = new ArrayList<>();

        /**
         * You add int to the Integer object ...
         * and still the code compiles
         */
        for (int i=0; i<10; i++) {
            li.add(i);
        }
    }
}
... 14 lines
˄˄˄

Random

Math.random works well when you need a single number. If you need to generate a series use java.util.Random.
 RunCode
class LearningApp {

    public static void main(String[] args) {
        
        int rand = (int) (Math.random()*100);

        System.out.println(rand);
    }
}
... 6 lines
˄˄˄
Task

 CompleteTask
class LearningApp {

    public static void main(String[] args) {
        
        /** 
         * Declare Number no as float 22.23 ...
         * Use object's shortValue() method and ...
         * Display no as 22
         */

        ...
    }
}
solution code
Questions    
No. 7   Strings
CARD
06
63

        A B C D E F
🔔
1/2