Results 1 to 3 of 3

Thread: Java syntax question

  1. #1

    Java syntax question

    i am trying to check a string to see if it is equal to "w" but it wont work.

    public File(String FileName, String mode){
    System.out.println(mode);
    if(mode == "w"){
    System.out.println(mode);
    OpenWrite(FileName) ;
    }
    else{
    System.out.println("not w");
    OpenRead(FileName) ;
    }
    }

    now i know by testing that "w" prints before the equilty statement, but then "not w" prints next, any help would be great.

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    lol
    http://java.sun.com/j2se/1.4.2/docs/...ng/String.html
    public File(String FileName, String mode){
    System.out.println(mode);
    if(mode.equals("w")){
    System.out.println(mode);
    OpenWrite(FileName) ;
    }
    else{
    System.out.println("not w");
    OpenRead(FileName) ;
    }
    }
    Strings are objects, you can only use == with primitive data types like int, double and boolean. If you're taking a class you will learn all about comparing objects, should be good for you . Peace.

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Actually h3r3tic, you are technically incorrect in saying that you can only use == with primitive data types. You "can" use == to compare objects, but with objects, == evaluates whether or not two objects are exactly the same; (as in same memory address, etc.) at least that's what I believe is the case.

    But hey, that was a pointless comment from me...just being pedantic I figure if I write this reply I might actually be able to get to sleep.

    ac

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •