Wednesday, October 5, 2016

Muhammad Akbar

Basic Data Types (Characters)-Tutorial

Basic Data Types (Characters)

  • In Java, to store characters, char primitive data type or Character class is used.
  • Java uses Unicode to represent characters. Unicode contains an international character set. As it contains such a huge data, it has a width of 16 bits. (http://www.unicode.org).
  • This makes Java a universal programming language.
  • The range of char : 0 to 65,536
  • Declare a Character using char primitive type
char d = 'S';
  • Declare a Character using Character Class
Character c = 'S';
  • Both produces same thing:
System.out.println(c.equals(d));
This prints true as both are same.

Methods in Characters


Method
Description
Result Data Type
Example
charValue
Convert the input to int (by casting).
Same for floatValue(), doubleValue() etc.
mentioned in the method  name
Character c = 'S';
System.out.println(c.charValue());
Prints: S
compareTo
Returns the value 0 if the argument Character is equal to this Character;
A value less than 0 if this Character is numerically less than the Character argument; A value greater than 0 if this Character is numerically greater than the Character argument (unsigned comparison).
Integer
Character c = 'S';
System.out.println(c.compareTo('E'));
Prints: 14
Character c = 'S';
System.out.println(c.compareTo('Z'));
Prints: -7
equals
Returns true if the objects are the same; false otherwise.

Boolean
Character c = 'S';
System.out.println(c.equals('S'));
System.out.println(c.equals('Z'))
Prints:
True
false
toString
Returns the String representation of the character object
String
Character c = 'S';
System.out.println(c.toString());
Prints: S
getNumericValue
Returns the int value that the specified Unicode character represents
Integer
Character c = 'S';
System.out.println(Character.getNumericValue(c));
Prints: 28
getType
Returns a value of type int representing the character's general category.

Integer
Character c = 'S';
System.out.println(Character.getType(c));
Prints: 1
isAlphabetic
Returns true if the character is an alphabet. If not, then returns false.
Boolean
Character c = 'S';
System.out.println(Character.isAlphabetic(c));
Character d = '1';
System.out.println(Character.isAlphabetic(d));

Prints:
True
false
isDigit
Returns true if the character is a digit. If not, then returns false.
Boolean
Character c = 'S';
System.out.println(Character.isDigit(c));
Character d = '1';
System.out.println(Character.isDigit(d));
Prints:
False
True
isLetterOrDigit
Returns true if the character is either a digit or an alphabet. If not, then returns false.
Boolean
Character c = 'S';
System.out.println(Character.isLetterOrDigit(c));
Character d = '%';
System.out.println(Character. isLetterOrDigit (d));
Prints:
True
false
isLowerCase
Returns true if the character is a lower case alphabet. If not, then returns false.
Boolean
Character c = 's';

System.out.println(Character.isLowerCase(c));
Character d = '%';
System.out.println(Character.isLowerCase(d));
Prints:
True
false
isUpperCase
Returns true if the character is an upper case alphabet. If not, then returns false.
Boolean
Character c = 'A';

System.out.println(Character.isUpperCase(c));
Character d = '%';
System.out.println(Character.isUpperCase(d));

Prints:
True
false
isSpaceChar
Returns true if the character is a Space. If not, then returns false.
Boolean
Character d = ' ';

System.out.println(Character.isSpaceChar(d));

Prints:
True
toUpperCase
Converts character to upper case
Character
Character c = 'a';

System.out.println(Character.toUpperCase(c));

Prints:
A
toLowerCase
Converts character to lower case
Character
Character d = 'H';

System.out.println(Character.toLowerCase(d));

Prints:
h
isWhiteSpace
Returns true if passed character is really a white space.
Boolean
Character a = ' ';
Character b = 'S';
Character c = '\n';   
System.out.println(Character.isWhitespace(a));
System.out.println(Character.isWhitespace(b));
System.out.println(Character.isWhitespace(c));
Prints:
True
False
true

Escape Characters in Java Characters

Sometime we can provide input to Java characters in a program which can be special characters like: new line, new tab, quotation marks etc. For those special characters, we need to make the compiler understand what they actually mean. Usually we use a back slash (\) as an escape sequence in that scenario.
Here is the actual table for the special characters that we need to handle carefully and differently in Java.
Escape Sequence
Meaning
\n
New Line
\t
New Tab
\b
Backspace
\’
Single Quote
\”
Double Quote
\\
Back Slash
\r
Carriage Return
\f
Form feed
Increase Char value
Though char is mainly used for handling characters, value of char can be increased by using simple + operator.
Example:
public static void main(String[] args) 
{
 Character a = 'A';
 System.out.println(a);
 a++;
 System.out.println(a);
}
Output:
A
B

Java Booleans

Boolean has only 2 possible values: true or false. And, it has very small number of methods associated with it. We have created a sample java program to describe the methods that are associated with Boolean variables in Java.
public static void main(String[] args) 
{
 Boolean a = true;
 
 System.out.println(a.toString()); // converts boolean variable to String
  
 System.out.println(a.compareTo(false)); // compares one boolean variable to another
  
 System.out.println(a.booleanValue()); // get a boolean value from a boolean variable
  
 System.out.println(Boolean.parseBoolean("false")); // take a string and if it is in a  format convert it to a boolean variable

}
The output is:
true
1
true
false

  

   Condition Statements in Java

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you