article

JAVA - 'Strings and Things' for absolute beginners

Email
Submitted on: 1/2/2015 6:50:00 PM
By: michael worthington (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: Java (JDK 1.2)
Views: 2333
 
     the purpose of this tutorial is to get beginner java programmers familiar with the variable type String, as well as with a couple String functions. it includes a simple program that beginners can make, and it covers a lot of basics to understanding strings and how one can use them.

 
				



INTRO TO JAVA (PART 3) - WRITTEN BY: M.WORTHINGTON [01.02.02]


...after checking out the first two tutorials you should you should now be able to make a running program as well as mess around with numbers. now it is time to endulge ourselves in the world of strings.

in our class StringsAndThings we will be using the variable type: String. the 's' must always be capitalized in the word 'String'. a String is any type of text. it can be letters, symbols, and even numbers. however, if the variable is defined as a String and it has a value of a number, it will not functino like a number, it will not function like an int or double. to explain, look at the following:
  String a = "1"; //correct
  String a = 1; //incorrect
  int a = "1"; //incorrect
  int a = 1; //correct
  String b = "2";
  String c = a + b;
   if you print out c, you would get 12 (1 and 2 is 12 in string format). c would not be 3 (1 + 2 = 3).

a key thing to know about Strings is that each letter or character of the string has an index. the indexes of a string go from 0 to the length of the string minus one. for example:
  String k = "Funky Town";
   Funky Town
   0123456789
The index of F is 0
The index of u is 1
The index of n is 2
The index of k is 3
The index of y is 4
The index of    is 5
The index of T is 6
The index of o is 7
The index of w is 8
The index of n is 9

The length of this string is 10, but as you can see, the last index is 9 (the length minus one). Always remember that the index starts at 0.

in this program, StringsAndThings we will be using string variables and some of their functions. they are as follows:
.length() <--- produces an integer of the length of the string
.substring(beginning position, ending position) <---prints out a specified part of the string
.toUpperCase() <---PUTS ALL OF THE CHARACTERS IN THE STRING TO UPPER CASE
.toLowerCase() <---puts all of the characters in the string to lower case

OBJECTIVE
to create, use, and manipulate Strings. this tutorial doesn't contain all of the useful functions that come along with Strings, but it does have the basics. i figured i'd get these out first for you to soak up, and i'll introduce others in another tutorial.

PROGRAM

public class StringsAndThings
{
  public static void main(String[] args)
  {
   String first = "John";
   String last = "Doe";
   String full = (first + " " + last);
   System.out.println("first name: " + first); // Line 1
   System.out.println("last name: " + last); // Line 2
   System.out.println("full name: " + full); // Line 3
   //length
   System.out.println("first name has " + first.length() + " characters"); // Line 4
   System.out.println("last name has " + last.length() + " characters"); // Line 5
   //substring
   System.out.println("last, first: " + full.substring(4,8) + ", " + full.substring(0,5)); //Line 6
   System.out.println("last, first: " + full.substring(4) + ", " + full.substring(0,5)); // Line 7
   //upper, lower cases
   System.out.println("first name: " + first.toUpperCase()); // Line 8
   System.out.println("first name: " + first); // Line 9
   first = first.toUpperCase();
   System.out.println("first name: " + first); // Line 10
   System.out.println("first name: " + first.toLowerCase()); // Line 11
   System.out.println("first name: " + first); // Line 12
   first = first.toLowerCase();
   System.out.println("first name: " + first); // Line 13
  } //main
} //class

ABOUT THE PROGRAM
on each line of coding that is going to print something out is a comment that has an assigned number line. this is just so referring to each line will be easier to understand.

OUTPUT
be sure to compile the program and then run it. refer back to the first tutorial for instructions.

the following is output from each line(minus the line reference) as you should have when you run the program:

Line 1:  first name: John
Line 2:  last name: Doe
Line 3:  full name: John Doe
Line 4:  first name has 4 characters
Line 5:  last name has 3 characters
Line 6:  last, first: Doe, John
Line 7:  last, first: Doe, John
Line 8:  first name: JOHN
Line 9:  first name: John
Line 10:  first name: JOHN
Line 11:  first name: john
Line 12:  first name: JOHN
Line 13:  first name: john


EXPLANATION
first, i want to jump straight to the upper and lower case section. while lines 8 and 11 printed out the first name in Upper and Lower Cases, respectively, they did not change what the variable itself contained. the contents of the variable do not change until after lines 9 and 12. lines 1 through 3 simply print out the contents of the variables. lines 4 and 5 simply print out the length of the strings. lines 6 and 7 print out substrings. line 6 prints out the characters at the 4th, 5th, 6th, and 7th indexes in the string, but not the 8th. the substring ends at the 8th index but does not include it. it then prints out the comma and the characters from 0 to 4, but not including the character with the index of 4. the only difference between lines 6 and 7 is that the first substring only has one number in the parenthesis. putting only one number in the substring parenthesis means 'print from this spot to the end of the string.' it is as simple as that. i suggest messing around with substrings until you completely have a grasp on them. they're not difficult, but simple mistakes with them will annoy you later down the road.

if you're still having trouble with the program and understanding it, your best bet is to just mess around with it. change up the variables, variable types (include ints and doubles in the program), values, and functions; before running the program, try to predict what is going to be printed out.

any other questions, hit up the message boards. if you chat there you can easily go ahead of tutorials and learn more programming.

please let me know what you think of this tutorial by going to the message boards or the contact page to submit your comments. thank you.


 


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.