• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assignment error  RSS feed

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.





 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!