Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

print out question  RSS feed

 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have a question.

Let's say i define a final int constant like this:



In this example, i will print out two 1's. If i want to print out the text house instead of the number 1, how do i do this? I realize i can always make a method with a return string, but i'm sure there's a smarter way? if java considers house = 1, then how do i swap the 1 with house in print out?
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is direct way to do this, what you just showed us here.
But you could have a House object which should encapsulate relevant with the house information and provide accordingly getter methods to retrieve that information.
example:
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,

Have you by chance programmed in FORTRAN? In FORTRAN you could say 1 = 2, then then if you had x = 1 + 1, then x would equal 4 and if you printed 1 you would actually get 2 printed. In some of todays scripting technologies you may have the ability to setup equivalencies where if you say house = 1, then the converse is true, but in Java what is happening is that a memory space is being set aside and a reference handle is given to it as "house", then the constant 1 is referenced by that handle. The constant 1 is completely unaffected, and to my knowledge there isn't any converse listing saying 1 is referenced by these handles, but there are listings that say these handles do reference these constants and variables.

Les


Tom Gibbins wrote:Hello, i have a question.

Let's say i define a final int constant like this:



In this example, i will print out two 1's. If i want to print out the text house instead of the number 1, how do i do this? I realize i can always make a method with a return string, but i'm sure there's a smarter way? if java considers house = 1, then how do i swap the 1 with house in print out?
 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Tom,

Have you by chance programmed in FORTRAN? In FORTRAN you could say 1 = 2, then then if you had x = 1 + 1, then x would equal 4 and if you printed 1 you would actually get 2 printed. In some of todays scripting technologies you may have the ability to setup equivalencies where if you say house = 1, then the converse is true, but in Java what is happening is that a memory space is being set aside and a reference handle is given to it as "house", then the constant 1 is referenced by that handle. The constant 1 is completely unaffected, and to my knowledge there isn't any converse listing saying 1 is referenced by these handles, but there are listings that say these handles do reference these constants and variables.

Les


Tom Gibbins wrote:Hello, i have a question.

Let's say i define a final int constant like this:



In this example, i will print out two 1's. If i want to print out the text house instead of the number 1, how do i do this? I realize i can always make a method with a return string, but i'm sure there's a smarter way? if java considers house = 1, then how do i swap the 1 with house in print out?


Hehe nope, java is my first language. But thanks for the clarification
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do the same in Forth
2 : 2 3 ; 2 + .
That way you can show that two and two make five.


The two 2s on their own put the value of 2 on the stack, which is added with the + operator and taken from the stack and displayed with the dot operator.
The colon takes you into compile mode and 2 is compiled to 3. The semicolon takes you back out of compile mode.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make house an enum:
enum MyItems { HOUSE, ... };
and print MyItems.HOUSE.name()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!