Java::New Qestion

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 663 Views - Last Post: 10 September 2010 - 03:46 AM Rate Topic: -----

Topic Sponsor:

#1 kevin_06s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 10

Java::New Qestion

Posted 09 September 2010 - 09:50 PM

In Java, what is the difference between
String s = new String("Hello World")
and
String s = "Hello World"
?
Is This A Good Question/Topic? 0
  • +

Replies To: Java::New Qestion

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 233
  • View blog
  • Posts: 656
  • Joined: 30-March 07

Re: Java::New Qestion

Posted 09 September 2010 - 10:03 PM

String s = new String("Hello World") basically creates a new instance of the the String object containing hello world
where as s = "hello world" dose basically the same thing but say like we create another String variable with the exact the same text java pools the memory meaning you have two separate variable but they both point to the same memory address.
Here is a simple program to kind of show what im trying to say:

public static void main(String[] args) {
        String s = "hello";
        String t = "hello";
        String r = new String("hello");
        System.out.println(s==r);
        System.out.println(s==t);
        System.out.println(s.equalsIgnoreCase(r));
    }

Output:
false
true
true

== is comparing the memory address where the s.equalsIgnoreCase( r ) compares the text its self.
System.out.println(s==t); is true because it points to the same memory where as System.out.println(s==r); is false because its hello is in a different memory location

This post has been edited by H3R3T1C: 09 September 2010 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 kevin_06s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 10

Re: Java::New Qestion

Posted 09 September 2010 - 10:11 PM

So then creating a new instance is the standard way to do it? I am guessing you would always use new unless you specifically want to pool the memory.
Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 168
  • View blog
  • Posts: 612
  • Joined: 02-December 09

Re: Java::New Qestion

Posted 09 September 2010 - 10:15 PM

Creating a String using the operator '= will create pointers to the same String in the Java String pool. Java conserves resources doing this because it does not need reserve memory for a new string which is the same as another that is already in memory. So using the operator '=' you will have two objects point to the same memory address. In fact if you use the '==' operator you will get true as result of the test. But when you use the keyword 'new' Java is forced to create separate strings so as Java compare two memory address they will be different from each other so using the operator '==' will return false as result.

I would say that is better use the operator '=' because every time that you use '=' you will be reusing the same memory location while if you use new all the time you will be allocating memory every time you use a String.
Was This Post Helpful? 0
  • +
  • -

#5 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 233
  • View blog
  • Posts: 656
  • Joined: 30-March 07

Re: Java::New Qestion

Posted 09 September 2010 - 10:18 PM

Its standard to set the value of your String object with the equals sign (String s = "sometext") and not using the = new String(<text>);
Also when comparing strings its standard to compare with .equalsIgnoreCase but you can also just use .equals if you want to check for case sensitive cases.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: Java::New Qestion

Posted 09 September 2010 - 10:25 PM

All of those answers are right, but not the full picture. That's why I wrote a tutorial explaining this. There is no "standard" way, but I definitely see the literal assignment fashion used more frequently.

Read my tutorial for more information:
http://www.dreaminco...he-string-pool/
Was This Post Helpful? 1
  • +
  • -

#7 kevin_06s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 10

Re: Java::New Qestion

Posted 09 September 2010 - 10:28 PM

Ok. Thanks. Now, getting out of strings for a moment, is it still standard to use the = operator when assigning user defined objects? In most tutorials I have seen, if they are assigning an object of a created class, new is used. Does this have anything to do with programming practices, or is it just something about constructors/initializing?
Was This Post Helpful? 0
  • +
  • -

#8 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 233
  • View blog
  • Posts: 656
  • Joined: 30-March 07

Re: Java::New Qestion

Posted 09 September 2010 - 10:36 PM

First of all very nice tutorial Dogstopper :D. +rep
I think that only String Class and Classes such as Long and Double that represent primitives can use just = with no 'new' to create new instances but where using User created class (and almost every java core class) you need to use 'new' to create a new instance of it. However if say you have something like this:
MyClass myClass;
MyClass myClass2 = new MyClass();
myClass = myClass2;


then you can assign myClass to myClass2 and you do not use 'new' because myClass2 has already been initialized and we just want to assign it to another variable. Both myClass and myClass2 point to the same memory location.

This post has been edited by H3R3T1C: 09 September 2010 - 10:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: Java::New Qestion

Posted 09 September 2010 - 10:54 PM

View PostH3R3T1C, on 10 September 2010 - 12:36 AM, said:

First of all very nice tutorial Dogstopper :D. +rep


Thanks :D

View PostH3R3T1C, on 10 September 2010 - 12:36 AM, said:

I think that only String Class and Classes such as Long and Double that represent primitives can use just = with no 'new' to create new instances but where using User created class (and almost every java core class) you need to use 'new' to create a new instance of it. However if say you have something like this:
MyClass myClass;
MyClass myClass2 = new MyClass();
myClass = myClass2;


then you can assign myClass to myClass2 and you do not use 'new' because myClass2 has already been initialized and we just want to assign it to another variable. Both myClass and myClass2 point to the same memory location.


To the extent of my knowledge the only CLASSES that can be instantiated without thee new keyword is String. The reason that Sun did that is because it is as commonly used as basic datatypes. That's why if you look through the code for String, a little bit of it is native. What Sun did is they used the power of C++ operator overloading and applied it to that object only for the reasons above. Sun still decided to leave operator overloading out of Java itself, but when they implemented String, they did utilize it in the JVM.

Long and Double as objects still use the "new" keyword. Only raw datatypes and Strings use literal assignments.

This post has been edited by Dogstopper: 09 September 2010 - 11:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 233
  • View blog
  • Posts: 656
  • Joined: 30-March 07

Re: Java::New Qestion

Posted 09 September 2010 - 10:58 PM

Classes representing primitives can be initialized without 'new'

public static void main(String[] args) {
        Double d = 1.2;
        Integer i = 1;
        Float f = 1.1f;
        Byte b =55;
        Long l = Long.MAX_VALUE;
        System.out.println(d);
        System.out.println(i);
        System.out.println(f);
        System.out.println(b);
        System.out.println(l);
    }
    

This post has been edited by Dogstopper: 09 September 2010 - 11:18 PM

Was This Post Helpful? 1
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: Java::New Qestion

Posted 09 September 2010 - 11:07 PM

View PostH3R3T1C, on 10 September 2010 - 12:58 AM, said:

Classes representing primitives can be initialized without 'new'

public static void main(String[] args) {
        Double d = 1.2;
        Integer i = 1;
        Float f = 1.1f;
        Byte b =55;
        Long l = Long.MAX_VALUE;
        System.out.println(d);
        System.out.println(i);
        System.out.println(f);
        System.out.println(b);
        System.out.println(l);
    }
    


Sure 'nouf...I completely forgot about autoboxing there for a minute! Same reason as String.

This post has been edited by Dogstopper: 09 September 2010 - 11:19 PM

Was This Post Helpful? 0
  • +
  • -

#12 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 233
  • View blog
  • Posts: 656
  • Joined: 30-March 07

Re: Java::New Qestion

Posted 09 September 2010 - 11:13 PM

Also forgot to add char bool and short(for anyone who is interested):
public static void main(String[] args) {
        Double d = 1.2;
        Integer i = 1;
        Float f = 1.1f;
        Byte b =55;
        Character c = 'c';
        Boolean bl = false;
        Short s = 127;
        Long l = Long.MAX_VALUE;
        System.out.println(d);
        System.out.println(i);
        System.out.println(f);
        System.out.println(b);
        System.out.println(l);
        System.out.println(c);
        System.out.println(bl);
        System.out.println(s);
    }


Edit: for some reason it keeps changing System.out.println(b ); to
System.out.println(B);
?! and no matter what i do it keeps capitalizing the b ?!

This post has been edited by H3R3T1C: 09 September 2010 - 11:19 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: Java::New Qestion

Posted 09 September 2010 - 11:17 PM

Just turn off Post emoticons...
B)

If you go into full editor and then click "Post Options", uncheck the "Enable Emoticons" button. I'll go fix your code post.
Was This Post Helpful? 0
  • +
  • -

#14 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 233
  • View blog
  • Posts: 656
  • Joined: 30-March 07

Re: Java::New Qestion

Posted 09 September 2010 - 11:20 PM

View PostDogstopper, on 10 September 2010 - 05:17 AM, said:

Just turn off Post emoticons...
B)

If you go into full editor and then click "Post Options", uncheck the "Enable Emoticons" button. I'll go fix your code post.

Aww ok Thanks :)
I feel like a total noob now :D
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: Java::New Qestion

Posted 09 September 2010 - 11:27 PM

View PostH3R3T1C, on 10 September 2010 - 01:20 AM, said:

View PostDogstopper, on 10 September 2010 - 05:17 AM, said:

Just turn off Post emoticons...
B)

If you go into full editor and then click "Post Options", uncheck the "Enable Emoticons" button. I'll go fix your code post.

Aww ok Thanks :)
I feel like a total noob now :D


Don't! You beat me out on programming Contest!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2