This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2017/1/9

Color

Nosson1459 Nosson1459

2017/1/9

#
What's the difference between "Color.white" and "Color.WHITE", and when should I use which?
danpost danpost

2017/1/9

#
Nosson1459 wrote...
What's the difference between "Color.white" and "Color.WHITE", and when should I use which?
Both are equally the same and can be used interchangeable. The lowercase names for the colors were first, which leads me to believe that they added the uppercase names later to conform with the standard convention of using all uppercase letters for constant values.
danpost danpost

2017/1/9

#
danpost wrote...
Both are equally the same
You can test this with:
1
System.out.println("\"'WHITE' is the same as 'white'\" is "+(java.awt.Color.WHITE.equals(java.awt.Color.white)));
Nosson1459 Nosson1459

2017/1/10

#
danpost wrote...
The lowercase names for the colors were first, which leads me to believe that they added the uppercase names later to conform with the standard convention of using all uppercase letters for constant values.
Then why didn't they delete the old ones, now there's both?
danpost wrote...
Both are equally the same
You can test this with:
1
System.out.println("\"'WHITE' is the same as 'white'\" is "+(java.awt.Color.WHITE.equals(java.awt.Color.white)));
I tried it like that and it said "true", when I changed one of them to Color.black it said "false", which means it works but when I changed the ".equals" to '==' I got the same results. Why did you use the .equals if you could use '==', it's not a string but it's not an int or double either? (What will happen if I use '.equals()' for an int?)
danpost danpost

2017/1/10

#
Nosson1459 wrote...
Then why didn't they delete the old ones, now there's both?
It would break all programs that already written using 'white'.
Why did you use the .equals if you could use '==', it's not a string but it's not an int or double either? (What will happen if I use '.equals()' for an int?)
Color objects are not a primitive type. This should show 'false':
1
System.out.println("\"'WHITE' is the same as 'Color(255, 255, 255)'\" is "+(java.awt.Color.WHITE == new java.awt.Color(255, 255, 255));
However, this should show 'true':
1
[code]System.out.println("\"'WHITE' is the same as 'Color(255, 255, 255)'\" is "+(java.awt.Color.WHITE.equals(new java.awt.Color(255, 255, 255)));
You cannot call a method on a primitive type. An 'int' value would need to be converted to an Integer object before any methods can be called on it.
Nosson1459 Nosson1459

2017/1/11

#
Oh. How do I know where to use '==' and where to use '.equals()'?
danpost danpost

2017/1/11

#
Nosson1459 wrote...
How do I know where to use '==' and where to use '.equals()'?
Since primitive types cannot use the 'equals' method, the question refers only to object references. If you want to know if two objects are similar, use the 'equals' method. If you want to know if two object references refer to the same object, use '=='. Apparently 'Color.white' and 'Color.WHITE' both refer to the same instance of the Color class.
1
2
3
4
5
6
7
// same object (both ways to compare them return 'true')
Color white = new Color(255, 255, 255);
Color WHITE = white;
 
// different objects (only 'equals' returns 'true')
Color white = new Color(255, 255, 255);
Color WHITE = new Color(255, 255, 255);
The Object class 'equals' method must be overridden in a class for comparing objects of the class for similarity with the 'equals' method. If not, 'equals' performs the same as '=='. All this is explained in the middle section of this page of the java tutorials.
Nosson1459 Nosson1459

2017/1/12

#
What is 'instanceof'? (you've used it with me before but I didn't know what it was/is)
danpost danpost

2017/1/12

#
Nosson1459 wrote...
What is 'instanceof'? (you've used it with me before but I didn't know what it was/is)
See the section called 'Instantiating a class' on this page of the java tutorials.
Nosson1459 Nosson1459

2017/1/13

#
danpost wrote...
Nosson1459 wrote...
What is 'instanceof'? (you've used it with me before but I didn't know what it was/is)
See the section called 'Instantiating a class' on this page of the java tutorials.
The word(s) 'instanceof' is mentioned 0 times on that page, I pressed Ctrl 'F' to "Find" and it said "0 of 0". (I did that after I looked through it.)
danpost danpost

2017/1/13

#
Nosson1459 wrote...
The word(s) 'instanceof' is mentioned 0 times on that page, I pressed Ctrl 'F' to "Find" and it said "0 of 0". (I did that after I looked through it.)
Sorry, try the bottom of this page.
Nosson1459 Nosson1459

2017/1/13

#
Thanks. (That's better.)
You need to login to post a reply.