SlideShare a Scribd company logo
1 of 23
Download to read offline
UNIVERSITY OF KUFA
COLLAGE OF COMPUTER SCIENCE AND
MATHEMATICS
Lecture 2
‫م‬
.
‫الحسن‬ ‫عبد‬ ‫هدى‬ ‫م‬
CHARACTER STRINGS
• A STRING LITERAL IS REPRESENTED BY PUTTING DOUBLE QUOTES AROUND THE TEXT
• EXAMPLES:
"THIS IS A STRING LITERAL."
"123 MAIN STREET"
"X"
THE PRINTLN METHOD
• THE PRINTLN METHOD TO PRINT A CHARACTER STRING
• THE SYSTEM.OUT OBJECT REPRESENTS A DESTINATION (THE MONITOR SCREEN) TO WHICH
WE CAN SEND OUTPUT
System.out.println ("Whatever you are, be a good one.");
object method
name
information provided to the method
(parameters)
THE PRINT METHOD
• THE SYSTEM.OUT OBJECT PROVIDES ANOTHER SERVICE AS WELL
• THE PRINT METHOD IS SIMILAR TO THE PRINTLN METHOD,
EXCEPT THAT IT DOES NOT ADVANCE TO THE NEXT LINE
• THEREFORE ANYTHING PRINTED AFTER A PRINT STATEMENT WILL
APPEAR ON THE SAME LINE
THE PRINT METHOD
//********************************************************************
// COUNTDOWN.JAVA AUTHOR: LEWIS/LOFTUS
//
// DEMONSTRATES THE DIFFERENCE BETWEEN PRINT AND PRINTLN.
//********************************************************************
PUBLIC CLASS COUNTDOWN
{
//-----------------------------------------------------------------
// PRINTS TWO LINES OF OUTPUT REPRESENTING A ROCKET COUNTDOWN.
//-----------------------------------------------------------------
PUBLIC STATIC VOID MAIN (STRING[] ARGS)
{
SYSTEM.OUT.PRINT ("THREE... ");
SYSTEM.OUT.PRINT ("TWO... ");
SYSTEM.OUT.PRINT ("ONE... ");
SYSTEM.OUT.PRINT ("ZERO... ");
SYSTEM.OUT.PRINTLN ("LIFTOFF!"); // APPEARS ON FIRST OUTPUT LINE
SYSTEM.OUT.PRINTLN ("HOUSTON, WE HAVE A PROBLEM.");
}
}
//********************************************************************
// Countdown.java Author: Lewis/Loftus
//
// Demonstrates the difference between print and println.
//********************************************************************
public class Countdown
{
//-----------------------------------------------------------------
// Prints two lines of output representing a rocket countdown.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.print ("Three... ");
System.out.print ("Two... ");
System.out.print ("One... ");
System.out.print ("Zero... ");
System.out.println ("Liftoff!"); // appears on first output line
System.out.println ("Houston, we have a problem.");
}
}
Output
Three... Two... One... Zero... Liftoff!
Houston, we have a problem.
STRING CONCATENATION
• THE STRING CONCATENATION OPERATOR (+) IS USED TO APPEND
ONE STRING TO THE END OF ANOTHER
"PEANUT BUTTER " + "AND JELLY"
• IT CAN ALSO BE USED TO APPEND A NUMBER TO A STRING
• A STRING LITERAL CANNOT BE BROKEN ACROSS TWO LINES IN A
PROGRAM
//********************************************************************
// Facts.java Author: Lewis/Loftus
//
// Demonstrates the use of the string concatenation operator and the
// automatic conversion of an integer to a string.
//********************************************************************
public class Facts
{
//-----------------------------------------------------------------
// Prints various facts.
//-----------------------------------------------------------------
public static void main (String[] args)
{
// Strings can be concatenated into one long string
System.out.println ("We present the following facts for your "
+ "extracurricular edification:");
System.out.println ();
// A string can contain numeric digits
System.out.println ("Letters in the Hawaiian alphabet: 12");
continue
continue
// A numeric value can be concatenated to a string
System.out.println ("Dialing code for Antarctica: " + 672);
System.out.println ("Year in which Leonardo da Vinci invented "
+ "the parachute: " + 1515);
System.out.println ("Speed of ketchup: " + 40 + " km per year");
}
}
Output
We present the following facts for your extracurricular edification:
Letters in the Hawaiian alphabet: 12
Dialing code for Antarctica: 672
Year in which Leonardo da Vinci invented the parachute: 1515
Speed of ketchup: 40 km per year
OUTPUT
24 AND 45 CONCATENATED: 2445
24 AND 45 ADDED: 69
//********************************************************************
// Addition.java Author: Lewis/Loftus
//
// Demonstrates the difference between the addition and string
// concatenation operators.
//********************************************************************
public class Addition
{
//-----------------------------------------------------------------
// Concatenates and adds two numbers and prints the results.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("24 and 45 concatenated: " + 24 + 45);
System.out.println ("24 and 45 added: " + (24 + 45));
}
}
QUICK CHECK
What output is produced by the following?
System.out.println ("X: " + 25);
System.out.println ("Y: " + (15 + 50));
System.out.println ("Z: " + 300 + 50);
QUICK CHECK
What output is produced by the following?
System.out.println ("X: " + 25);
System.out.println ("Y: " + (15 + 50));
System.out.println ("Z: " + 300 + 50);
X: 25
Y: 65
Z: 30050
Escape Sequences
• What if we wanted to print the quote character?
• The following line would confuse the compiler because it would
interpret the second quote as the end of the string
System.out.println ("I said "Hello" to you.");
• An escape sequence is a series of characters that represents a
special character
• An escape sequence begins with a backslash character ()
System.out.println ("I said "Hello" to you.");
Escape Sequences
• Some Java escape sequences:
Escape Sequence
b
t
n
r
"
'

Meaning
backspace
tab
newline
carriage return
double quote
single quote
backslash
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// Roses.java Author: Lewis/Loftus
//
// Demonstrates the use of escape sequences.
//********************************************************************
public class Roses
{
//-----------------------------------------------------------------
// Prints a poem (of sorts) on multiple lines.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("Roses are red,ntViolets are blue,n" +
"Sugar is sweet,ntBut I have "commitment issues",nt" +
"So I'd rather just be friendsntAt this point in our " +
"relationship.");
}
}
OUTPUT
ROSES ARE RED,
VIOLETS ARE BLUE,
SUGAR IS SWEET,
BUT I HAVE "COMMITMENT ISSUES",
SO I'D RATHER JUST BE FRIENDS
AT THIS POINT IN OUR RELATIONSHIP.
Variables
• A variable is a name for a location in memory that holds
a value
• A variable declaration specifies the variable's name and
the type of information that it will hold
int total;
int count, temp, result;
Multiple variables can be created in one declaration
data type variable name
Copyright © 2012 Pearson Education, Inc.
Variable Initialization
• A variable can be given an initial value in the
declaration
int sum = 0;
int base = 32, max = 149;
Copyright © 2012 Pearson Education, Inc.
• When a variable is referenced in a program, its
current value is used
//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************
public class PianoKeys
{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************
public class PianoKeys
{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
Output
A piano has 88 keys.
ASSIGNMENT
• AN ASSIGNMENT STATEMENT CHANGES THE VALUE OF A VARIABLE
• THE ASSIGNMENT OPERATOR IS THE = SIGN
Copyright © 2012 Pearson Education, Inc.
total = 55;
• The value that was in total is overwritten
• You can only assign a value to a variable that is
consistent with the variable's declared type
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// Geometry.java Author: Lewis/Loftus
//
// Demonstrates the use of an assignment statement to change the
// value stored in a variable.
//********************************************************************
public class Geometry
{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");
sides = 10; // assignment statement
System.out.println ("A decagon has " + sides + " sides.");
sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// Geometry.java Author: Lewis/Loftus
//
// Demonstrates the use of an assignment statement to change the
// value stored in a variable.
//********************************************************************
public class Geometry
{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");
sides = 10; // assignment statement
System.out.println ("A decagon has " + sides + " sides.");
sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}
Output
A heptagon has 7 sides.
A decagon has 10 sides.
a dodecagon has 12 sides.

More Related Content

Similar to Java Print method

Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5DanWooster1
 
6Modify the bfs.java program (Listing A) to find the minimu.docx
6Modify the bfs.java program (Listing  A) to find the minimu.docx6Modify the bfs.java program (Listing  A) to find the minimu.docx
6Modify the bfs.java program (Listing A) to find the minimu.docxevonnehoggarth79783
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfARCHANASTOREKOTA
 
Numerical Data And Expression
Numerical Data And ExpressionNumerical Data And Expression
Numerical Data And ExpressionPRN USM
 
Endevor api an introduction to the endevor application programming interface
Endevor api   an introduction to the endevor application programming interface Endevor api   an introduction to the endevor application programming interface
Endevor api an introduction to the endevor application programming interface Kevin Grimes
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3Sónia
 
Lab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docxLab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docxDIPESH30
 
CC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfCC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfsecunderbadtirumalgi
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfarmyshoes
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsDanWooster1
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTEr. Ganesh Ram Suwal
 
java-introduction.pdf
java-introduction.pdfjava-introduction.pdf
java-introduction.pdfDngTin307322
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 

Similar to Java Print method (20)

Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5
 
6Modify the bfs.java program (Listing A) to find the minimu.docx
6Modify the bfs.java program (Listing  A) to find the minimu.docx6Modify the bfs.java program (Listing  A) to find the minimu.docx
6Modify the bfs.java program (Listing A) to find the minimu.docx
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 
Ifi7107 lesson4
Ifi7107 lesson4Ifi7107 lesson4
Ifi7107 lesson4
 
Numerical Data And Expression
Numerical Data And ExpressionNumerical Data And Expression
Numerical Data And Expression
 
Endevor api an introduction to the endevor application programming interface
Endevor api   an introduction to the endevor application programming interface Endevor api   an introduction to the endevor application programming interface
Endevor api an introduction to the endevor application programming interface
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
 
Lab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docxLab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docx
 
CC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfCC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdf
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
 
Vo.pdf
   Vo.pdf   Vo.pdf
Vo.pdf
 
Bioinformatica p4-io
Bioinformatica p4-ioBioinformatica p4-io
Bioinformatica p4-io
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LIST
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
Java
JavaJava
Java
 
java-introduction.pdf
java-introduction.pdfjava-introduction.pdf
java-introduction.pdf
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
Lesson 5
Lesson 5Lesson 5
Lesson 5
 

More from Huda Alameen

More from Huda Alameen (20)

Architectural design
Architectural designArchitectural design
Architectural design
 
System Modeling
System ModelingSystem Modeling
System Modeling
 
Requirements Engineering
Requirements EngineeringRequirements Engineering
Requirements Engineering
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Softweare Engieering
Softweare Engieering Softweare Engieering
Softweare Engieering
 
Softweare Engieering
Softweare Engieering Softweare Engieering
Softweare Engieering
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Sql viwes
Sql viwesSql viwes
Sql viwes
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Normalization
NormalizationNormalization
Normalization
 
Lecture one db
Lecture one dbLecture one db
Lecture one db
 
Introduction to structured query language
Introduction to structured query languageIntroduction to structured query language
Introduction to structured query language
 
Indexing techniques
Indexing techniquesIndexing techniques
Indexing techniques
 
Agg fun
Agg funAgg fun
Agg fun
 
Se lec1 (1)
Se lec1 (1)Se lec1 (1)
Se lec1 (1)
 
Se lec6
Se lec6Se lec6
Se lec6
 
Se lec5
Se lec5Se lec5
Se lec5
 
Se lec 4
Se lec 4Se lec 4
Se lec 4
 
Se lec 3
Se lec 3Se lec 3
Se lec 3
 

Recently uploaded

Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Work, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE PhysicsWork, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE Physicsvishikhakeshava1
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​kaibalyasahoo82800
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxAleenaTreesaSaji
 
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfWadeK3
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzohaibmir069
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Cultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxCultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxpradhanghanshyam7136
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 

Recently uploaded (20)

Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Work, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE PhysicsWork, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE Physics
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptx
 
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistan
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Cultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxCultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptx
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 

Java Print method

  • 1. UNIVERSITY OF KUFA COLLAGE OF COMPUTER SCIENCE AND MATHEMATICS Lecture 2 ‫م‬ . ‫الحسن‬ ‫عبد‬ ‫هدى‬ ‫م‬
  • 2. CHARACTER STRINGS • A STRING LITERAL IS REPRESENTED BY PUTTING DOUBLE QUOTES AROUND THE TEXT • EXAMPLES: "THIS IS A STRING LITERAL." "123 MAIN STREET" "X"
  • 3. THE PRINTLN METHOD • THE PRINTLN METHOD TO PRINT A CHARACTER STRING • THE SYSTEM.OUT OBJECT REPRESENTS A DESTINATION (THE MONITOR SCREEN) TO WHICH WE CAN SEND OUTPUT System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters)
  • 4. THE PRINT METHOD • THE SYSTEM.OUT OBJECT PROVIDES ANOTHER SERVICE AS WELL • THE PRINT METHOD IS SIMILAR TO THE PRINTLN METHOD, EXCEPT THAT IT DOES NOT ADVANCE TO THE NEXT LINE • THEREFORE ANYTHING PRINTED AFTER A PRINT STATEMENT WILL APPEAR ON THE SAME LINE
  • 5. THE PRINT METHOD //******************************************************************** // COUNTDOWN.JAVA AUTHOR: LEWIS/LOFTUS // // DEMONSTRATES THE DIFFERENCE BETWEEN PRINT AND PRINTLN. //******************************************************************** PUBLIC CLASS COUNTDOWN { //----------------------------------------------------------------- // PRINTS TWO LINES OF OUTPUT REPRESENTING A ROCKET COUNTDOWN. //----------------------------------------------------------------- PUBLIC STATIC VOID MAIN (STRING[] ARGS) { SYSTEM.OUT.PRINT ("THREE... "); SYSTEM.OUT.PRINT ("TWO... "); SYSTEM.OUT.PRINT ("ONE... "); SYSTEM.OUT.PRINT ("ZERO... "); SYSTEM.OUT.PRINTLN ("LIFTOFF!"); // APPEARS ON FIRST OUTPUT LINE SYSTEM.OUT.PRINTLN ("HOUSTON, WE HAVE A PROBLEM."); } }
  • 6. //******************************************************************** // Countdown.java Author: Lewis/Loftus // // Demonstrates the difference between print and println. //******************************************************************** public class Countdown { //----------------------------------------------------------------- // Prints two lines of output representing a rocket countdown. //----------------------------------------------------------------- public static void main (String[] args) { System.out.print ("Three... "); System.out.print ("Two... "); System.out.print ("One... "); System.out.print ("Zero... "); System.out.println ("Liftoff!"); // appears on first output line System.out.println ("Houston, we have a problem."); } } Output Three... Two... One... Zero... Liftoff! Houston, we have a problem.
  • 7. STRING CONCATENATION • THE STRING CONCATENATION OPERATOR (+) IS USED TO APPEND ONE STRING TO THE END OF ANOTHER "PEANUT BUTTER " + "AND JELLY" • IT CAN ALSO BE USED TO APPEND A NUMBER TO A STRING • A STRING LITERAL CANNOT BE BROKEN ACROSS TWO LINES IN A PROGRAM
  • 8. //******************************************************************** // Facts.java Author: Lewis/Loftus // // Demonstrates the use of the string concatenation operator and the // automatic conversion of an integer to a string. //******************************************************************** public class Facts { //----------------------------------------------------------------- // Prints various facts. //----------------------------------------------------------------- public static void main (String[] args) { // Strings can be concatenated into one long string System.out.println ("We present the following facts for your " + "extracurricular edification:"); System.out.println (); // A string can contain numeric digits System.out.println ("Letters in the Hawaiian alphabet: 12"); continue
  • 9. continue // A numeric value can be concatenated to a string System.out.println ("Dialing code for Antarctica: " + 672); System.out.println ("Year in which Leonardo da Vinci invented " + "the parachute: " + 1515); System.out.println ("Speed of ketchup: " + 40 + " km per year"); } } Output We present the following facts for your extracurricular edification: Letters in the Hawaiian alphabet: 12 Dialing code for Antarctica: 672 Year in which Leonardo da Vinci invented the parachute: 1515 Speed of ketchup: 40 km per year
  • 10. OUTPUT 24 AND 45 CONCATENATED: 2445 24 AND 45 ADDED: 69 //******************************************************************** // Addition.java Author: Lewis/Loftus // // Demonstrates the difference between the addition and string // concatenation operators. //******************************************************************** public class Addition { //----------------------------------------------------------------- // Concatenates and adds two numbers and prints the results. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("24 and 45 concatenated: " + 24 + 45); System.out.println ("24 and 45 added: " + (24 + 45)); } }
  • 11. QUICK CHECK What output is produced by the following? System.out.println ("X: " + 25); System.out.println ("Y: " + (15 + 50)); System.out.println ("Z: " + 300 + 50);
  • 12. QUICK CHECK What output is produced by the following? System.out.println ("X: " + 25); System.out.println ("Y: " + (15 + 50)); System.out.println ("Z: " + 300 + 50); X: 25 Y: 65 Z: 30050
  • 13. Escape Sequences • What if we wanted to print the quote character? • The following line would confuse the compiler because it would interpret the second quote as the end of the string System.out.println ("I said "Hello" to you."); • An escape sequence is a series of characters that represents a special character • An escape sequence begins with a backslash character () System.out.println ("I said "Hello" to you.");
  • 14. Escape Sequences • Some Java escape sequences: Escape Sequence b t n r " ' Meaning backspace tab newline carriage return double quote single quote backslash Copyright © 2012 Pearson Education, Inc.
  • 15. //******************************************************************** // Roses.java Author: Lewis/Loftus // // Demonstrates the use of escape sequences. //******************************************************************** public class Roses { //----------------------------------------------------------------- // Prints a poem (of sorts) on multiple lines. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("Roses are red,ntViolets are blue,n" + "Sugar is sweet,ntBut I have "commitment issues",nt" + "So I'd rather just be friendsntAt this point in our " + "relationship."); } }
  • 16. OUTPUT ROSES ARE RED, VIOLETS ARE BLUE, SUGAR IS SWEET, BUT I HAVE "COMMITMENT ISSUES", SO I'D RATHER JUST BE FRIENDS AT THIS POINT IN OUR RELATIONSHIP.
  • 17. Variables • A variable is a name for a location in memory that holds a value • A variable declaration specifies the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name Copyright © 2012 Pearson Education, Inc.
  • 18. Variable Initialization • A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; Copyright © 2012 Pearson Education, Inc. • When a variable is referenced in a program, its current value is used
  • 19. //******************************************************************** // PianoKeys.java Author: Lewis/Loftus // // Demonstrates the declaration, initialization, and use of an // integer variable. //******************************************************************** public class PianoKeys { //----------------------------------------------------------------- // Prints the number of keys on a piano. //----------------------------------------------------------------- public static void main (String[] args) { int keys = 88; System.out.println ("A piano has " + keys + " keys."); } }
  • 20. //******************************************************************** // PianoKeys.java Author: Lewis/Loftus // // Demonstrates the declaration, initialization, and use of an // integer variable. //******************************************************************** public class PianoKeys { //----------------------------------------------------------------- // Prints the number of keys on a piano. //----------------------------------------------------------------- public static void main (String[] args) { int keys = 88; System.out.println ("A piano has " + keys + " keys."); } } Output A piano has 88 keys.
  • 21. ASSIGNMENT • AN ASSIGNMENT STATEMENT CHANGES THE VALUE OF A VARIABLE • THE ASSIGNMENT OPERATOR IS THE = SIGN Copyright © 2012 Pearson Education, Inc. total = 55; • The value that was in total is overwritten • You can only assign a value to a variable that is consistent with the variable's declared type
  • 22. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Geometry.java Author: Lewis/Loftus // // Demonstrates the use of an assignment statement to change the // value stored in a variable. //******************************************************************** public class Geometry { //----------------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------------- public static void main (String[] args) { int sides = 7; // declaration with initialization System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); } }
  • 23. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Geometry.java Author: Lewis/Loftus // // Demonstrates the use of an assignment statement to change the // value stored in a variable. //******************************************************************** public class Geometry { //----------------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------------- public static void main (String[] args) { int sides = 7; // declaration with initialization System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); } } Output A heptagon has 7 sides. A decagon has 10 sides. a dodecagon has 12 sides.