yt

Header Ads

Chapter 8 What is DataTypes with String Explain in Details

Data Types is Important Part of any Programming Language, So in this post discuss on details what is Datatypes and how many types with how to declared datatype in JAVA. So lets go start now.

What is Data Types in Java:- 

  • It is specifiers the type of data that a variable can store such as integers, float, characters, string, etc.
  • In another word : It is a type of data which is used  in the program.
  • There are many pre-defined data types in JAVA library like int, char, float,string etc.  

Types of  DataType in Java


1. Primitive DataTypes : 

  • These data Types which is pre- defined in the library is called primitive data type.
  • It is named by a keyword for example int, float, char, long, dooble etc.
  • This datatype are fastest from another and used in directly in store of value so fastest. 
  • These are also called Built-in-DataTypes.
  • There are 8 Primitive datatypes in java. For example:- byte, short, int, long, float, double, boolean and char

1.      Integer Type :- These data types store only Whole Numbers.

Types

Size in bytes

Range

byte

1

-128 to 127

short

2

-32,768 to 32,767

int

4

-2,147,483,648 to 2,147,483,647

long

8

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808


Float Types :- Store Fractional

Types

Size in bytes

                                         Range

float

4

3.4e-038 to 3.4e+0.38.

double

8

1.7e-308 to 1.7e+038.

       Boolean Types

Keyword

boolean

Syntax

boolean  x,

Value

True/ False

Default

False


Character 

Keyword

 

char

Syntax

 

char x;

Value

 

‘a’ , ‘@’, ‘9’

Default

 

0 to65536


2. Non- primitive Data Types
  • These Data Types which is derived from Primitive Data Types is called non- primitive data types. 
  • It is also called refrence data type.
  • The don't store the value, but store a refrence to that value. 
  • Example:- Array, class, interface,function, etc. 

Introduction to java strings

Strings characters  की sequence  होती है। जैसे की "Java in Hindi" एक string  है|  Java main strings objects ko तरह implement किया गया है। ऐसा इसलिए किया गया है, ताकि String पर Operations perform किये जा सके। Operations जैसे की 2 strings को जोड़ना या compare करना आदि,आप strings पर perform कर सकते है। String एक class है। आप normal string variables भी create कर सकते है और string class का object भी create कर सकते है। और दोनों के साथ ही operations perform कर सकते है।

For Example :- 
String myString = "this is a string";// variable
String MyString = new String ("This is a string"); // object of class

String को एक variable की तरह और एक object की तरह use करने में फर्क होता है। जब आप string को object की तरह यूज़ करते है तो एक बार object create होने के बाद आप उसमे कोई changes नहीं कर सकते है। उदाहरण के लिए ऊपर दिए हुए example को देखिये मैने MyString नाम से object बनाया है। लेकिन अब इसके text "This is a string" में कोई change नहीं किया जा सकता है. लेकिन जब आप string को variable की तरह यूज़ करते है तो उसमे आसानी से changes किये जा सकते है।

Java कुछ methods provide करती है, जिनसे आप string object में modification कर सकते है। लेकिन जब भी आप ऐसा करते है तो एक नया string object generate a Original string object में कोई change नहीं आता है। इन सभी operations के बारे में निचे दिया जा रहा है।

Java strings

String length
  • किसी भी string में जितने characters होते है वो उस string की length होते है। किसी भी string की length पता करने के लिए आप length() method यूज़ कर सकते है। इसके लिए आप उस string पर length method call करते है।
String myString = "hello"; System.out.printin(myString.length());  

Concatenating strings
  • जब किन्हीं 2 strings को जोड़कर एक तीसरी string बनायीं जाती है, तो उसे string concatenation कहते है। ऐसा आप + operator के द्वारा भी कर सकते है और concat() method के द्वारा भी कर सकते है। इन दोनों ही तरीकों के उदाहरण निचे दिए जा रहे है।
String myString = "This is a string"; String yourString = "This is your string";
String ourString = myString+yourString; // with + ourString = operator

myString.concat(yourString); // with concat() method
System.out.printIn(ourString);

Java strings String indexing with charAt()
  • ये method string में से किसी एक method को extract करने के लिए यूज़ किया जाता है। Strings की indexing arrays की तरह ही होती है। 1st String  character 0 index पर होता है। इसलिए आप string के किसी भी single character को access कर सकते है। इसके लिए आप charAt () method यूज़ करते है। जो index number आप इस method में pass करते है ये method उसी index के character को return  करता है।
String myString = "hello";
System.out.printIn(myString.charAt ( 3 ) );

String indexing with substring()
  • किसी string में sub string search करने के लिए आप substring () method यूज़ करते है। इस method में आप 2 arguments pass करते है। पहला argument वो index number है, जंहा से आप string को access करना चाहते है।और दूसरा argument वो index number है, जँहा तक की string आप access करना चाहते है। ये method starting index से लेकर ending index तक की string return करता है।
String s1 = "hello my name is khan"; 
System.out.println(s1.substring(5,8));

String indexing with indexOf()
  • कौन सी sub string किस index पर है ये पता करने के लिए आप index Of() method यूज़ करते है। इस method में एक sub string pass की जाती है। ये method उस sub string stating index return करता है।
String s1 = "hello my name is khan"; 
System.out.println(s1.in dexOf("my"));
                 //returns 7

String comparison with equals() 
  • कोई भी 2 strings को compare करने के लिए Equals () method यूज़ किया जाता है। यदि दोनों strings equal होती है तो ये method true return करता है। एक string पर इस method को call किया जाता है और दूसरी string argument की तरह pass की  जाती है। 
  • इसका उदाहरण निचे दिया जा रहा है।
String s1 = "hello"; 
String s2 = "hello";
System.out.println(s1.equals(s2)); 
                // prints true.

No comments

Theme images by Dizzo. Powered by Blogger.