Java Programming: Strings, Command Line Arguments, and Data Types, Lecture notes of Web Design and Development

Various aspects of java programming, including string concatenation and comparison, taking command line arguments, and the difference between primitives and objects. It includes example code and explanations.

Typology: Lecture notes

2011/2012

Uploaded on 11/10/2012

taariq
taariq 🇵🇰

4.4

(16)

61 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Handout 3-1
Web Design & Development CS-506
- 27 -
Learning Basics
Strings
A string is commonly considered to be a sequence of characters stored in memory and
accessible as a unit. Strings in java are represented as objects.
String Concatenation
“+” operator is used to concatenate strings
System.out.pritln(“Hello” + “World”) will print Hello World on console
String concatenated with any other data type such as int will also convert that
datatype to String and the result will be a concatenated String displayed on console.
For example,
int i = 4;
int j = 5;
System .out.println (“Hello” + i)
will print Hello 4 on screen
However
System,.out..println( i+j) ;
will print 9 on the console because both i and j are of type int.
Comparing Strings
For comparing Strings never use == operator, use equals method of String class.
== operator compares addresses (shallow comparison) while equals compares
values (deep comparison)
E.g string1.equals(string2)
Example Code: String concatenation and comparison
public class StringTest {
public static void main(String[] args) {
int i = 4;
int j = 5;
System.out.println("Hello" + i); // will print Hello4
System.out.println(i + j); // will print 9
String s1 = new String (“pakistan”);
String s2 = “pakistan”;
if (s1 == s2) {
System.out.println(“comparing string using == operator”);
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Programming: Strings, Command Line Arguments, and Data Types and more Lecture notes Web Design and Development in PDF only on Docsity!

Web Design & Development CS-

Learning Basics

Strings

A string is commonly considered to be a sequence of characters stored in memory and

accessible as a unit. Strings in java are represented as objects.

String Concatenation

ƒ “+” operator is used to concatenate strings

– System.out.pritln(“Hello” + “World”) will print Hello World on console

ƒ String concatenated with any other data type such as int will also convert that

datatype to String and the result will be a concatenated String displayed on console.

For example,

– int i = 4;

– int j = 5;

ƒ System .out.println (“Hello” + i)

ƒ will print Hello 4 on screen

– However

ƒ System,.out..println( i+j) ;

ƒ will print 9 on the console because both i and j are of type int.

Comparing Strings

For comparing Strings never use == operator, use equals method of String class.

– == operator compares addresses (shallow comparison) while equals compares

values (deep comparison)

ƒ E.g string1.equals(string2)

Example Code: String concatenation and comparison

public class StringTest { public static void main(String[] args) {

int i = 4; int j = 5;

System.out.println("Hello" + i); // will print Hello System.out.println(i + j); // will print 9

String s1 = new String (“pakistan”); String s2 = “pakistan”;

if (s1 == s2) { System.out.println(“comparing string using == operator”); }

Web Design & Development CS-

if (s1.equals( s2) ) { System.out.println(“comparing string using equal method”); } } }

On execution of the above program, following output will produce

Web Design & Development CS-

Output

C:\java AnyArgsApp i can pass any number of arguments

Argument:0 value i

Argument:1 value can

Argument:2 value pass

Argument:3 value any

Argument:4 value number

Argument:5 value of

Argument:6 value arguments

Web Design & Development CS-

Primitives vs Objects

  • Everything in Java is an “Object”, as every class by default inherits from class

“Object” , except a few primitive data types, which are there for efficiency

reasons.

  • Primitive Data Types

o Primitive Data types of java

ƒ boolean, byte Æ 1 byte

ƒ char, short Æ 2 bytes

ƒ int, float Æ 4 bytes

ƒ long, double Æ 8 bytes

  • Primitive data types are generally used for local variables, parameters and

instance variables (properties of an object)

  • Primitive datatypes are located on the stack and we can only access their value,

while objects are located on heap and we have a reference to these objects

  • Also primitive data types are always passed by value while objects are always

passed by reference in java. There is no C++ like methods

  • void someMethod(int &a, int & b ) // not available in java

Stack vs. Heap

Stack and heap are two important memory areas. Primitives are created on the stack

while objects are created on heap. This will be further clarified by looking at the

following diagram that is taken from Java Lab Course.

num

st

0F

0F

name ali

Stack Heap

int num = 5;

Student s = new Student();

Web Design & Development CS-

The following table summarizes the parser methods available to a java programmer.

Data Type Convert String using either … byte Byte.parseByte( string^ ) new Byte( string^ ).byteValue() short Short.parseShort( string^ ) new Short( string^ ).shortValue() int Integer.parseInteger( string^ ) new Integer( string^ ).intValue() long Long.parseLong( string^ ) new Long( string^ ).longValue() float Float.parseFloat( string^ ) new Float( string^ ).floatValue() double Double.parseDouble( string^ ) new Double( string ).doubleValue()

Web Design & Development CS-

Example Code: Taking Input / Output

So far, we learned how to print something on console. Now the time has come to learn

how to print on the GUI. Taking input from console is not as straightforward as in C++.

Initially we’ll study how to take input through GUI (by using JOPtionPane class).

The following program will take input (a number) through GUI and prints its square on

the console as well on GUI.

  1. import javax.swing.*;
  2. public class InputOutputTest {
  3. public static void main(String[] args) {
  4. //takes input through GUI
  5. String input = JOptionPane.showInputDialog("Enter number");
  6. int number = Integer.parseInt(input);
  7. int square = number * number;
  8. //Display square on console
  9. System.out.println("square:" + square);
  10. //Display square on GUI
  11. JOptionPane.showMessageDialog(null, "square:"+ square);
  12. System.exit(0);

On line 1, swing package was imported because it contains the JOptionPane class that

will be used for taking input from GUI and displaying output to GUI. It is similar to

header classes of C++.

On line 5, showInputDialog method is called of JOptionPane class by passing string

argument that will be displayed on GUI (dialog box). This method always returns back a

String regardless of whatever you entered (int, float, double, char) in the input filed.

Our task is to print square of a number on console, so we first convert a string into a

number by calling parseInt method of Integer wrapper class. This is what we done on line

number 6.

Line 11 will display square on GUI (dialog box) by using showMessageDialog method of

JOptionPane class. The first argument passed to this method is null and the second

argument must be a String. Here we use string concatenation.

Web Design & Development CS-

Selection & Control Structure

The if-else and switch selection structures are exactly similar to we have in C++. All

relational operators that we use in C++ to perform comparisons are also available in java

with same behavior. Likewise for, while and do-while control structures are alike to C++.