Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java Strings and StringBuffers looked at  (Read 1972 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2012-03-20 00:25:18 »

Wow just figured this out, before you read further down try thinking what the method below is going to outprint.
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void test() {
      String s = "hello";
      String s1 = new String("hello");
      if (s == s1 || s1 == s) {
         System.out.println("TRUE");
      } else if (s != s1) {
         System.out.println("FALSE");
      }
   }


Results: "FALSE".

I'm watching a series on youtube here:
http://www.youtube.com/watch?v=zHF0g-PDO5E&feature=related
(Random episode i'm watching).

The video pertaining to that method I created above is 'Strings & StringBuffers'.

Basicly saying that instead of:
1  
String s = "hello";


You should use this to save the JVM from creating more memory weight:
1  
String s1 = new String("hello");


Or, if you were to go:
1  
2  
3  
4  
5  
6  
   public String test() {
      String s = "hello";
      String s1 = "world";
      String s2 = s += s1;
      return s2;
   }


It would save more space on the JVM/memory if you were to basically just complete the next method it's going to perform.
(Saving the JVM some heap memory)
1  
2  
3  
4  
5  
6  
   public String test() {
      String s = new String("hello");
      String s1 = new String("world");
      String s2 = new StringBuffer("hello").append("world").toString();
      return s2;
   }


Basically because with this method:
1  
2  
3  
4  
5  
6  
   public String test() {
      String s = "hello";
      String s1 = "world";
      String s2 = s += s1;
      return s2;
   }


It's creating 2 Memory Heaps/Storing Memory for each String, because out of those Strings if it involves 's1 += s2' it than has to go through Java's StringBuffer class, as seen in the method above.

Hope this helps somebody out, looks like i'm going to start smart coding.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-03-20 00:56:09 »

StringBuffer is unnecessarily synchronized.  Use StringBuilder instead, which IIRC is what javac is itself using now for string concatenations (that lesson was probably written before StringBuilder existed).
Offline GabrielBailey74
« Reply #2 - Posted 2012-03-20 01:03:52 »

Wish there was a way to actually tell how much heap or how many stacks you were creating.
I'd sit here comparing methods all day o.O

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-03-20 01:11:12 »

You can use a heap profiler to profile the heap, but this example is ridiculously short-lived, and isn't going to eat any appreciable amount of memory unless you're doing this with strings that are hundreds of megs -- and then it's more "WTF are you doing making them single strings".  

Stack is something you don't need to worry about unless you're a) running out of it, or b) spawning hundreds of threads.

It's good to be aware of the costs of doing naive string concatenation, but since java started using StringBuffer/StringBuilder instead of the super-naive algorithm in most cases, it hasn't been all that big a deal.
Offline GabrielBailey74
« Reply #4 - Posted 2012-03-20 01:24:55 »

This is what fascinated me:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void test() {
      String s = "hello";
      String s1 = new String("hello");
      if (s == s1 || s1 == s) {
         System.out.println("TRUE");
      } else if (s != s1) {
         System.out.println("FALSE");
      }
   }


I deal with methods using a String peram all the time:
1  
2  
3  
4  
public void getName(String name) {
      if(name=="lol") {
            doLol();
      }


How can that return false..
String s = "lol";
String q = new String("lol");
or vice versa:
String s1 = "lol";
String s2 = new String(s1);

s IS = q;

and s1 IS = s2 which is = s1;

Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-03-20 01:29:26 »

String literals are interned, and will share the same reference if they're the same literal string, but strings created through the String constructor never are, so they're a unique object every time.  Same goes for any string that's computed in any way that the compiler can't optimize away into a pure constant.  This is why you always use .equals() to compare strings, never ==.
Offline GabrielBailey74
« Reply #6 - Posted 2012-03-20 01:32:43 »

Well I just learned something new:
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public static void test() {
      String s1 = "lol";
      String s2 = new String(s1);
      if (s1.equals(s2)) {
         System.out.println("TRUE");
      } else if (!s1.equals(s2)) {
         System.out.println("FALSE");
      }
   }


And now it returns True, thank you mate.

Offline Danny02
« Reply #7 - Posted 2012-03-20 06:30:12 »

Strings are just objects in JAva, take that example
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
class Stuff
{
    public static final Stuff ZERO = new Stuff(0);
    private int value;
    public Stuff(int v){value=v;}
    public boolean equal(Stuff other){return value == other.value;}
}

Stuff z = Stuff.ZERO;
Stuff z1 = new Stuff(0);

z == z1; //<= will be fals, because those are two different object references
z.equals(z1);// will be true, because both hold the same value, the equals method is checking for
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!