Java char data type problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rameshwar Soni
    New Member
    • May 2011
    • 3

    Java char data type problem

    Can anyone please tell me why i am getting an error in the following code?

    Code:
    class Char_Demo
    {
    	public static void main(String[] n)
    	{
    // we all know we can store a character literal like A or //B in an char variable
    
    char c='A';
    System.out.println("Character c has" +c);   //output is A
    		
    	// we can even store the ASCII value for A
    		
    char c1=65;
    System.out.println("Character c1 has" +c1 );//output is A
    
    
    	//But now i want to store a number as a character		
    		
    	char c2='5';	// 5 stored as a character	System.out.println("Character c2 has" +c2 );//output is 5
    
    // but when i store a number which has more then 1 digit //i get an compile-time error
    
    char c3='12';		// error....why this error comes
    System.out.println("Character c3 has" +c3 );
        }
    }
    ]
    Last edited by Dheeraj Joshi; Jul 28 '11, 08:24 AM. Reason: Fixed Code tags
  • Dominik Przybys
    New Member
    • Jul 2011
    • 8

    #2
    There is an error because in Java we have char datatype containing only one char. If you want to store more than one char, you have to use String class.

    Comment

    • Rameshwar Soni
      New Member
      • May 2011
      • 3

      #3
      Thanks for the reply........I now have understood my mistake........ ............Tha nks

      Comment

      Working...