-4

The following line of code will not compile:

String 89.9 = new String("Hot 89.9");

It gives the following errors:

src/mainPackage/Userinterface.java:73: error: ';' expected
        String Hot89.9 = new String("Hot 89.9");
                    ^

src/mainPackage/Userinterface.java:73: error: invalid method declaration; return type required
        String Hot89.9 = new String("Hot 89.9");
                             ^

src/mainPackage/Userinterface.java:73: error: illegal start of type
        String Hot89.9 = new String("Hot 89.9");

Am I doing something wrong when I am creating my string?

Jens
  • 63,364
  • 15
  • 92
  • 104
Jared
  • 19
  • 1
  • 3

10 Answers10

3
  • You cannot create a variable name starting with numeric's.
  • You cannot use period with in the variable name in Java.

Try to change it to something else

String hot89_9= new String("Hot 89.9");

Period have a special meaning in Java (dot operator)

Suresh Atta
  • 118,038
  • 37
  • 189
  • 297
2

Variable name cannot begin with a number, or contain '.' in it.

String s89_9 = new String("Hot 89.9");
bigGuy
  • 1,692
  • 1
  • 22
  • 36
0

You cannot have a period (dot) in a variable name

If you really want you can try this

String s89_9 = new String("Hot 89.9");
David Pilkington
  • 13,297
  • 3
  • 39
  • 67
0

Yes, you try to use a period (.) in a variable name. That is NOT allowed.

stealthjong
  • 10,502
  • 13
  • 43
  • 83
0

You can't have a variable name with "." and the best practice to declare a variable name is to use lower case for the first character

Thiagz
  • 109
  • 1
  • 12
0

U cannot use dot (".") for naming in Java. Dot is for methods and properties:

Integer.valueOf("123");
String name = worger.name;    
Toro Boro
  • 377
  • 5
  • 15
0

No, a dot is not allowed in an identifier name. Only special characters you can use is a $ and _

Checkout the naming rules for identifiers here

Aman Gautam
  • 3,499
  • 2
  • 20
  • 25
0

You cannot use .(dot) in a variable name. Only alphanumeric characters and _ (underscore) are allowed in the variable name.

Amal
  • 278
  • 1
  • 11
0

A small note, creating a new String object is worse in perfomance as opposed to just directly giving in the string.

engineercoding
  • 832
  • 6
  • 14