Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Simple assignment question.  (Read 1321 times)
0 Members and 1 Guest are viewing this topic.
Offline jessewclark

Senior Newbie

Heaven means muzak.

« Posted 2004-01-18 18:06:28 »

I'm trying to examine an array of strings and find out which is the widest.  Here's an approximation of the code I'm using:

// The set of texts:

String [] texts = a bunch of texts

Font [] fonts = the fonts of those texts

// A variable to hold the longest line, initially set to the width of the first line:

double longestLine = ( the width of the first line, calculated using the font and the getStringBounds method) ;

// A variable to hold the current width when counting through the texts:

double currentWidth ;

// Now, counting through the texts to find the longest one:

for ( int i=1 ; i < texts.length ; i ++ ) {

     currentWidth =  ( again using getStringBounds, this time to find the width of the current line ) ;

     if ( longestLine < currentWidth )

          longestLine = new Double ( currentWidth ).doubleValue () ;


So, here's my assignment question.  What I really want to do in that last line is:

    longestLine = currentWidth ;

except I suspect that will make longestLine and currentWidth both pointers to the same variable, and thus longestLine will change whenever currentWidth changes.  That's not what I want.  But using "new Double ( currentWidth ).doubleValue ()" seems like a really dumb way to do things.  I mean, really: creating a Double from a double so that I can get a new double?  That seems really roundabout.  Is there a simpler way to do the same thing?
Offline princec

JGO Kernel

Medals: 343
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #1 - Posted 2004-01-18 18:23:53 »

You have become madly confused about how primitives work. It doesn't make a reference to any data at all. It just copies that value into the variable, just like you probably always thought it would!

In other words
longestLine = currentWidth;

is all you want to do on that last line.

Cas Smiley

Offline jessewclark

Senior Newbie

Heaven means muzak.

« Reply #2 - Posted 2004-01-18 18:29:54 »

Huh.  Okay, so, but let's just say we were talking about a Point2D and not a double.  In that case,

pointA = pointB

would make both pointA and pointB point to the same object, instead of copying pointB into a new object for pointA, right?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel

Medals: 343
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #3 - Posted 2004-01-18 18:31:37 »


Cas Smiley

Offline jessewclark

Senior Newbie

Heaven means muzak.

« Reply #4 - Posted 2004-01-18 18:32:00 »

Offline cfmdobbie

Senior Member

Medals: 1

Who, me?

« Reply #5 - Posted 2004-01-18 22:40:38 »

And to finish that thought, that situation is why the Sun Gods gave us the Object.clone() method.  If you have a Point2D object and instead of assigning a second reference to it you want a reference to an identical but different object, call:

 Point2D pointB = (Point2D)pointA.clone();

Hellomynameis Charlie Dobbie.
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.

E.R. Fleming (15 views)
2014-07-29 03:07:13

E.R. Fleming (5 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22 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‑
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!