03_strings_input_else - Java 211 Strings Reading Input...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Java 211 Strings, Reading Input, Else-If Yosef Mendelsohn I. Strings The String data type is not one of the primitive data types (e.g. int, char, double, etc) that we have been discussing over the last couple of lcctures. They are in fact, objects (which you will better understand later in the course). This distinction will become important. For now, we will begin with some String fundamentals and then learn more about them as we learn about objects. The best way to describe a string is to give some examples: String s1 = "hello"; //a string of length=5 String s2 = "hello, Bob. . How are you? "; String s3 = "h"; //a string of length=1 String s4 = "123456"; //a string of digits – it is NOT a number) Notice that the data type String begins an upper case ‘S’. However: char c1 = ‘h’; if (c1 == s3) //error!!! – type mismatch int x = s4; //error – type mismatch Some commonly used methods that can be invoked by a string object include: - equals (returns a bool) - length (returns an int) - toLowerCase (returns a String) The idea of an object invoking its own method may confuse you. Again, we will discuss this at a later point. For now, we will concern ourselves with simply USING these methods. You will have to take my word on a few things for now. I will explain the reasoning behind them as we progress. To compare two strings you can NOT use the == operator. The == operator works ONLY for primitive data types (eg to compare two variables of type int or double).
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Recall that strings are not primitive data types. So we need another way to compare two strings. Here is the an example of some code to compare two strings: String s1 = "hello"; String s2 = "goodbye"; To compare these two strings to see if they are identical, we would write: s1.equals(s2) To use this inside an ‘if’ statement: if ( s1.equals(s2) ) { System.out.println("The strings are identical"); } Another example: if ( s1.equals(“hello”) ) { System.out.println("The string s1 holds the word hello. "); } The ‘equals’ method is very important. Be sure that you are very comfortable with its basic usage. Another very useful String method is called length() . Here it is in use: String s1 = "hello"; int length = s1.length(); System.out.println("The length of the string is: " + length ); Or: if ( s1.length() > 10 ) { System.out.println("The string s1 is longer than 10 characters. "); } Notice the need for a pair of parentheses after the word length
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

03_strings_input_else - Java 211 Strings Reading Input...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online