• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignment error

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) short can fit in double. When I assign "d = s3" , I am getting error "Syntax error on token ";", , expected" on the line immediately before this assignment (int s3 = s2). If I comment "d = s3", this error is gone. Whats wrong with this assignment ? I am getting same error, if I do "s2 = s1" or "f = s2"

This error does not come, if I am creating new declaration. "float f5 = s2" works fine.

So, once a primitive variable is declared, can it be not be assigned value in the same scope ? Does it have to be assigned a value in a method only ? When I assign s2 = s1 in aMethod, it works fine.

2) Another thing is, long l takes more bytes than float f5. But when I assign "f5 = l", I don't get type cast error. Why is that ? If I do "i = l" , I immediately get type cat error. Why do I not get type cast error for assigning a float variable a value of a long ?

Thanks
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your second question check out: http://www.java-forums.org/new-java/37007-long-float-conversion.html

Apparently, float can hold larger numbers than long, but there are errors introduced because of the fact that it rounds.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:
1) short can fit in double. When I assign "d = s3" , I am getting error "Syntax error on token ";", , expected" on the line immediately before this assignment (int s3 = s2). If I comment "d = s3", this error is gone. Whats wrong with this assignment ? I am getting same error, if I do "s2 = s1" or "f = s2"

This error does not come, if I am creating new declaration. "float f5 = s2" works fine.

So, once a primitive variable is declared, can it be not be assigned value in the same scope ? Does it have to be assigned a value in a method only ? When I assign s2 = s1 in aMethod, it works fine.



Java code, that is not a declaration, can only be in a constructor, initializer, or method.

Henry
 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About question one "f,f1,i,d,j,b,l,s1,s2,s3" all will be treated as instance variables and always remember instance variables have to be either initialized at the point of declaration or assigned *inside* a method.


You cannot have a statement like "d = s3" line outside the scope of a method.





 
reply
    Bookmark Topic Watch Topic
  • New Topic