• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Declare a variable with characters and numbers?  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to create a variable which allows letters and numbers to be stored in the same instance.

for example

int age
String name
??? login

login = mypassword112321

is such a variable even in existence?
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Time for you to learn about Classes and Objects
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know those java tutorials are supposed to mean something, I can't decide what.

has anyone got a simple english answer?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mypassword112321" is just a string. The fact that it contains decimal digit characters is not significant. So you don't need a type that stores both letters and numbers, just a string.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok Thanks

My understanding was Stings could only hold letters and int could only hold numbers.

look at that I learned something

cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings can contain any character: letters, numbers, punctuation...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also be aware that the string "213" is not a number, it is a string of characters (that just happen to be decimal digits). This is very different from an int that holds the number 213.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An int does not hold a number. An int is a number. Plain and simple. If you write
int i = 123;
there will be a memory location with 123 (or more precisely 0x0000007b) in.

There are various ways to ensure a String contains certain elements; look for regular expressions as one way to verify that.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this may be a little beyond what the OP was asking, but if I write

Integer myInt = 123;

There will again be a memory location somewhere with 123 (or more precisely 0x0000007b) in it.

yes?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
“Yes and no” was my first thought.
What you have is a compile‑time constant 123 which is assigned (via boxing) to an Integer. That Integer is a reference type not a primitive so its memory location (or its memory handle more likely) is stored behind the variable myInt. Maybe that would have been better as myInteger because it isn't an int any more. But that Integer object has a value (or similar) field and that field stores the value 0x0000007b (=123).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:“Yes and no” was my first thought.

where does the "no" come in? the value has to be stored SOMEWHERE in memory. you may not have direct access to it, but that isn't the issue.

Regardless of how the value of the Integer variable is set (and my apologies for the poor choice of name), the number 123 has to be stored somewhere in memory.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . where does the "no" come in?. . .
I said it was my first thought. I didn't take long for me to realise the no was mistaken.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!