-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
  • 67,715
  • 15
  • 98
  • 113
jarpoole
  • 19
  • 1
  • 4

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
  • 120,458
  • 37
  • 198
  • 307
2

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

String s89_9 = new String("Hot 89.9");
bigGuy
  • 1,732
  • 1
  • 22
  • 37
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,528
  • 3
  • 41
  • 73
0

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

stealthjong
  • 10,858
  • 13
  • 45
  • 84
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
  • 121
  • 1
  • 13
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
  • 16
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,549
  • 2
  • 21
  • 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