Java problems (1 Viewer)

OP
Zambrotta

Zambrotta

Senior Member
Nov 16, 2001
2,421
  • Thread Starter
  • Thread Starter #5
    Does any of your suggestions actually work?

    Is this a correct way to do it? (Can't test it myself on this computer unfortunately)

    int r = Math.random()*100
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #6
    Does any of your suggestions actually work?
    I just tested this:

    Code:
    import java.util.Random;
    
    class Jrand {
    
        public static void main(String[] args) {
            Random r = new Random();
            int randint = r.nextInt(10);
            System.out.println(randint);
        }
    
    }
    from the first url I posted
     
    OP
    Zambrotta

    Zambrotta

    Senior Member
    Nov 16, 2001
    2,421
  • Thread Starter
  • Thread Starter #17
    "Probably" :D super user do, it basically gives access to any command, kind of like the key to the heavens :D I would call it the blue pill at certain days
    Difficult stuff. :)


    Another java question.

    When I use
    If statements sometimes I'm supposed to use ( == ) but sometimes people tell me it's better to have ( .equals ). I've never understood when.
     
    Oct 1, 2002
    2,089
    #18
    Difficult stuff. :)


    Another java question.

    When I use
    If statements sometimes I'm supposed to use ( == ) but sometimes people tell me it's better to have ( .equals ). I've never understood when.
    You need to know the differences between primitives and objects in java. When you want to compare value between primitives you can use == operator. while the same case cannot be done with object.

    The easiest example in this case is String,
    String str1 = new String("value1");// create new object
    String str2 = new String("value1");// create another new object
    System.out.println(str1 == str2);/* will print false, because it compares whether str1 and str2 refers to same object.*/
    System.out.println(str1.equals(str2));/* will print true, because the value of this two objects are equal*/
     

    Users Who Are Viewing This Thread (Users: 0, Guests: 1)