-1

I was looking for a program that could return only numbers from a string containing letters and numbers and found the code below.
The program works perfectly, but introduces Character. To my understanding char is a single letter, number or any other sign in java, but I have never heard of Character. Is it the same as char or is it completely different? How do I use it (other than the way shown below)?

and what do I need to do if I want the print in int?

String something = "423e";
int length = something.length();
String result = "";
for (int i = 0; i < length; i++) {
    Character character = something.charAt(i);
    if (Character.isDigit(character)) {
        result += character;
    }
}
System.out.println("result is: " + result);
  • 4
    Do you know the difference between int and Integer or booean and Boolean or long and Long ... ? They just wraps a value of the primitive type. – StackFlowed Sep 11 '14 at 17:04

6 Answers6

3

Character is a wrapper class for char, since char is a primitive type and thus isn't an Object. it fulfills the same role as Integer and Boolean.

Edit: It also provide a convenient class to store methods that deal with chars, like isDigit() in your example.

Sizik
  • 1,026
  • 5
  • 11
1

Character:

The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

For example, isDigit() is a functionality provided by the wrapper class Character.

Character.isDigit(character)
BatScream
  • 17,549
  • 4
  • 38
  • 60
1

char int are primitive data types where as Character/Integer implement Object class. Java provides wrapper class Character for primitive data type char. Please check details here

shrishinde
  • 2,592
  • 16
  • 25
0

The answer is these just wrap a value of the primitive type.

StackFlowed
  • 6,575
  • 1
  • 26
  • 41
0

Character is a class that wraps an char as an object. Here is a link with more detail. http://docs.oracle.com/javase/tutorial/java/data/characters.html

0

Primitives are not object in java , So in order to treat them as object java provides wrapper classes

sol4me
  • 13,975
  • 5
  • 31
  • 32