Raghu
Raghu

Reputation: 31

Unclosed character literal in java

I'm new to java and when I tried to print basic hello world program. I am getting following error "Error:(11, 12) java: unclosed character literal "

package com.company;

public class Main {
public static void main(String[] args) {

    //declare a variable

    char a;
    a = 'helloworld';
    System.out.println("type the char value: " + a);
 }
}

Can you please let me know where did I made a mistake ?

Upvotes: 0

Views: 13609

Answers (3)

Shyam Ghodasra
Shyam Ghodasra

Reputation: 41

First of all you have to understand all the datatype.

char is use for only store one char

String is use for store many char

1st way

String a = "helloWorld";

2nd way

char a[] = {'h', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};

for(char c : a)
     System.out.print(c);

3rd way

System.out.println("HelloWorld");

among all these 1st one is perfect

Upvotes: 1

thegauravmahawar
thegauravmahawar

Reputation: 2823

Single quotes can only take one character.

Change the data type to String

String a = "helloworld";

String represents a string of characters.

See docs: String and Data types

Upvotes: 3

VortixDev
VortixDev

Reputation: 1013

First off, the "char" data type can only take a single character. What you're looking for is the "String" data type.

Secondly, as a note on the first, if you create a String you'll need to use double quotes "" rather than single quotes ''.

Upvotes: 1

Related Questions