September 04, 2010, 04:12:39 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: [Basic] Working with String's  (Read 573 times)
0 Members and 1 Guest are viewing this topic.
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« on: August 07, 2009, 03:17:08 PM »

I know this is basic and most people here do know this, and how to create string's, but it's for people who are new to java and didnt know this.

Alright, a string is basically a sequance of characters, they are mostly used for messages in java, a prime example would be the hello world program, such as the message: "Hello, World!" which is displayed in the helloApp.

In java, string's are pretty wierd, they do not define strings as a primitive type, but instead strings are the referance type defined by the java API String class.

I am know goin to talk to you about how these string are used.

Java's string-handling features are pretty advanced and fun to learn.
Strings are declared and initialized much like primiative types like i said before, well im know going to show you the difference the only actually diffrence is the word "String" is capitlezed unlike the keywords for the primitive types such as int and double etc....
The reason for that is String isnt actually a keyword, instead it's the name of the Java API class that provides for string objects.

The following statements define and initilize a string variable:

Code:
String s;
s = "Hello, Wolrd!";

in this case the variable names "s" of type String is declared and initilized with the String statment "Hello, World!", please notice that string laterals are enclosed un quotation marks, not apostrophes so dont make the silly mistake people do.
Apostrophes are used for character  literals, please note* these are diffrent from string literals.

Like any varible declaration, a string declaration can include an initilizertherefore you can declare and initilize a string varible in one statement, like this:

Code:
String s = "Hello, Wolrd!";

Class varabiles and instance varibles are automatically initilzed to empty strings, but local varables are not duh..

alright im now gona show you how to initilize a local string varible to an emptey string, use a statement like this:

Code:
String s = " ";

Combining Strings

Combining two strings can be done by using the Plus sign (+), this is the Concatenation operator.
I am now going to give you an example using the hello world app.

Code:
String hello = "Hello, ";
String wolrd = "World!";
String greeting = hello + world;


I hope you have learnt something new if you feel i have made a spelling mistake or an error please let me know, thanks wink

Ikram
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Javaforums.net :: a community about Java software development.
« on: August 07, 2009, 03:17:08 PM »

 Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« Reply #1 on: August 07, 2009, 03:21:10 PM »


Code:
String s = "Hello, Wolrd!";
this is not an empty string , String s = ""; is, your string holds a space and although it doesn't show anything it's not empty.

Code:
String hello = "Hello, ";
String wolrd = "World!";
String greeting = hello + world;
This will give a compile error.

Ikram


rep given though, nice one! I'll write a few more advanced tutorials myself shortly such as threads, datastructure and MVC patterns just to name a few.
« Last Edit: August 07, 2009, 03:22:52 PM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
   

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.175 seconds with 33 queries.

Google visited last this page Yesterday at 09:50:01 AM