Results 1 to 5 of 5
Thread: Literals and variables
- 05-16-2012, 05:18 AM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Literals and variables
Hello everyone,
I am "New to Java" and have a VBA background. I bought 2 books on Java and am looking forward to writting my first program/Mobile App.
I am a bit confused about the literals and variable concept and it might have to do with my very recent introduction to VBA.
That said, here's my dilemma:
In VBA, once I declare a varialbe (Dim Age As Long) VBA know that this is Long and processes the variable as such. However, in Java, it looks like I have to suffix a long with an L or a 1 even though the Syntax states "Long X = 23452312;". Why do I need an L to indicate that the variable "X" in a long, when it's already declared as a long on the left side of the statement? Are variable types independent of the literal types in Java? If so, can someone please elaborate on that concept.
I appreciate all the help you provide and I am looking forward to a rich learning experience in the Java language.
Best regards,
S.
- 05-16-2012, 06:00 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Literals and variables
The java compilers sees numbers without decimals as an integer data type unless specified differently...given an int and long are different data types, specifying the L lets the compiler know otherwise. For instance,
The same can be said for other primitive data types (for instance float and double).Java Code:long myLong = 2341242341123412;//should throw a compile time error ... long myLong = 2341242341123412L;//lets the compiler know the number is a long
- 05-16-2012, 06:35 AM #3
- 05-16-2012, 09:59 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 05-17-2012, 05:12 AM #5
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Java's Defualt Literal Types (Whole number literals defaults to int?)
By Jomar808 in forum New To JavaReplies: 3Last Post: 02-14-2012, 10:45 PM -
I can only print string literals
By leeavital in forum New To JavaReplies: 1Last Post: 12-11-2009, 01:20 AM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
Variables
By mew in forum New To JavaReplies: 3Last Post: 12-11-2007, 12:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks