17 Replies - 663 Views - Last Post: 10 September 2010 - 03:46 AM
#1
Java::New Qestion
Posted 09 September 2010 - 09:50 PM
String s = new String("Hello World")
and
String s = "Hello World"
?
Replies To: Java::New Qestion
#2
Re: Java::New Qestion
Posted 09 September 2010 - 10:03 PM
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
#3
Re: Java::New Qestion
Posted 09 September 2010 - 10:11 PM
#4
Re: Java::New Qestion
Posted 09 September 2010 - 10:15 PM
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.
#5
Re: Java::New Qestion
Posted 09 September 2010 - 10:18 PM
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.
#6
Re: Java::New Qestion
Posted 09 September 2010 - 10:25 PM
Read my tutorial for more information:
http://www.dreaminco...he-string-pool/
#7
Re: Java::New Qestion
Posted 09 September 2010 - 10:28 PM
#8
Re: Java::New Qestion
Posted 09 September 2010 - 10:36 PM
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
#9
Re: Java::New Qestion
Posted 09 September 2010 - 10:54 PM
H3R3T1C, on 10 September 2010 - 12:36 AM, said:
Thanks
H3R3T1C, on 10 September 2010 - 12:36 AM, said:
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.
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
#10
Re: Java::New Qestion
Posted 09 September 2010 - 10:58 PM
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
#11
Re: Java::New Qestion
Posted 09 September 2010 - 11:07 PM
H3R3T1C, on 10 September 2010 - 12:58 AM, said:
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
#12
Re: Java::New Qestion
Posted 09 September 2010 - 11:13 PM
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
#13
Re: Java::New Qestion
Posted 09 September 2010 - 11:17 PM
B)
If you go into full editor and then click "Post Options", uncheck the "Enable Emoticons" button. I'll go fix your code post.
#14
Re: Java::New Qestion
Posted 09 September 2010 - 11:20 PM
#15
|
|

New Topic/Question
Reply




MultiQuote






|