SlideShare a Scribd company logo
CSPP51036
Java Programming
By- Darshan Gohel
Who is this class for?
 There is no assumption that you know any
Java or other OO programming language. If
you do, you may find the first few weeks
boring.
 You must have programming experience.
We will not spend very much time on
syntax, procedural constructs, operators,
etc. It is assumed that you can pick this up.
History of Java
 First version released in 1995
 Eight major versions released since then
– JDK 1.0 (1996) JDBC, Distributed Objects
– JDK 1.1 (1997) New Event Model
– J2SE 1.2 (1998) Swing
– J2SE 1.3 (2000) Cleanup
– J2SE 1.4 (2002)
– J2SE 5.0 (2004) Generics
– J2SE 6.0 (2006)
– J2SE 7.0 (2011)
Five Design Principles of Java
 simple, object-oriented and familiar
 robust and secure
 architecture-neutral and portable
 execute with "high performance
 interpreted, threaded, and dynamic
What is/isn’t Java?
 Read chapter 1 of Core Java
– Discussion is very balanced
 Basically, compared to C Java is a relatively high-
level language with many built-in features for
portably doing useful things such as:
– Multithreading
– Writing distributed programs
– Writing GUI clients
– Error handling
– Extending Web servers
– Embedding programs in Web browsers
– Connecting to commercial databases
Programming with Java
Java vs. C
 Equally importantly, Java has many core
language features that make it much more
natural to express abstraction, develop
software frameworks, etc.
 Also, many core language features which
ease debugging and promote reuse.
Compiling/running first java
program
 Create source code file (call it for example
MyFirstProgram.java).
 To compile:
prompt >> javac MyFirstProgram.java
 This produces byte code file named
MyFirstProgram.class
 To run:
prompt >> java MyFirstProgram
Observations
 .class file is not machine code. It is intermediate
form called Java Byte code. Can run on any
platform as long as platform has a Java Virtual
Machine (JVM).
 The second step on previous slide invokes the
JVM to interpret the byte code on the given
platform.
 In theory, byte code can be moved to another
platform and be run there without recompiling –
this is the magic of applets.
 Leave off the .class part when invoking the JVM.
Observations
 This is an old-fashioned command-line
program. Java also supports GUI
applications and web-browser hosted
programs called applets.
 After the first couple of weeks we will use
graphical rather than scripting front-ends.
Writing first program
 To keep things simple our first few
programs will all be written as just a single
main program.
 In java, the file in which your program
resides must contain a .java extension (e.g.
MyFirstProgram.java).
Writing first program
 Then, the program must be wrapped in a
class definition which is the same as the file
basename (MyFirstProgram). Careful, Java
is case-sensitive.
class MyFirstProgram { … }
 Finally, main is defined similar to C, but
with a few more modifiers:
public static void main(String[] args){ … }
These are all required. No shortcuts.
Writing first program
 Just as in C, main(..) is the principle entry point
into the program. When you say
java Program
Java looks in Program for a procedure named
main. This is where the program starts.
 To print to stdout in java use:
System.out.println(“ …”);
 MyFirstProgram.
Basic Programming
Constructs
What you should learn on your own
Breakdown of a java program
 Strategy to start is write evertything in a
single main program and very quickly
review the basics of java syntax (very little
time here).
 Then we break into procedures.
 Then class/packages.
Single-threaded program
 For a single thread of execution, each line
of code is executed sequentially (as in C).
 Each statement is terminated with a
semicolon.
 Unlike C, declarations can occur anywhere
within a program.
 Basic operators, control statements almost
exactly like C. A few minor differences.
 Best to just look at some examples.
Java Data Types
 Sizes fully specified by Java standard.
 Java is a very strongly typed language
 Integer types
– int (4 bytes signed)
– short (2 bytes signed)
– long (8 bytes signed) use suffix L (eg 1000000000L)
– byte (1 byte signed)
 Floating-point types
– float (4 bytes) use suffix F (eg 1.28F)
– double( 8 bytes)
Additional Data Types
 char
– Two-byte unicode
– Assignment with ‘ ‘
• e.g. char c = ‘h’;
 boolean
– true or false
e.g. boolean x = true;
if (x){…};
Operators/Control Flow
 Almost exactly like regular ANSI C.
 +, *, -, /, %, ++, --, +=, etc.
 ==, !=, >, < , etc.
 if statements, for loops, while loops, do loops,
switch statements, etc.
 continue, break, return, System.exit(0).
 Read pp 54– in Core Java.
 No need to spend class time going over these.
Scoping
 Braces are used as in C to denote begin/end of
blocks
 Be careful of the following:
int j = 0;
if ( j <1 ){
int k = 2;
…
}
k = 3; //Error! k inaccessible here
 Declarations do not propogate upwards.
Adding datatypes -- classes
 Java has handful of built-in datatypes just
discussed (int, float, etc.)
 Just like in C, user typically creates own
homemade datatypes to work with particular
application (ie structs and enums).
 In Java these are called classes.
 Many class definitions come as a standard part of
the Java distribution. Most common Example is
String class.
Strings
 Java provides a class definition for a type called
String
 Since the String class is part of the java.lang
package, no special imports are required to use it
(like a header file in C).
 Just like regular datatypes (and like C), variables
of type String are declared as:
String s1;
String s2, s3; //etc.
 Note that String is uppercase. This is the Java
convention for classnames.
Strings
 Initializing a String is painless
s1 = “This is some java String”;
 Note that double quotes are required.
 Memory is allocated dynamically.
 Think of above method as shortcut for more
standard way (assuming s1 has been declared):
s1 = new String(“This is some java String”);
 new operator required to create memory for new
String object.
String methods
 Given a String object we can then access any
public String method or instance variable (field).
 Best to think of analogy with C. Given a variable
of some struct type, we can access any of the
struct’s members. If one of these members is a
pointer to a function, we can essentially call a
function using the struct. (x.doit(x,…))
 In Java, this idea is taken quite a bit further, but
the above analogy is a good start.
String Examples
 Best to see by way of example:
String s = new String(“Hello”);
Char c = s.charAt(3);
System.out.println(c);
 Method charAt called on String object s
taking single integer parameter.
 How might this look in a procedural
language with structures? (homework)
String class documentation
 Incredibly important!
– Each standard java class definition is fully documented
online
– You must become facile at reading/interpreting these
documents. This is how everything is done in Java.
– A little hard at first but then very simple. Makes
learning new functionality much easier (if it’s well
written).
– Make a link to
http://java.sun.com/j2se/1.3/docs/api/index.html
 Much of homework will come from interpreting
this page.
1d Arrays
 Arrays in Java are dynamic; they are
allocated with the new operator.
 Creating a (1d) array is a two-step process:
int[] x; //declare x to be an array of ints
//x has the value of null right now
x = new int[100]; //allocate 100 ints worth
 At this point the values of x are all zeroes.
 Assignment is then just like C.
1d Arrays
 Note that in Java, unlike C, the compiler will not
let you overrun the bounds of an array. Give it a
try.
 Note that you must use the new operator to size
the array. It cannot be done statically as in C.
Until this is done, the array reference has a value
of null.
 The array comes with a field called length which
stores the number of elements in the array. Try
printing x.length in the previous example. This is a
simple but nice convenience.
Misc tricks to get work done
Parsings Strings
 Recall that the args array is an array of Strings.
Thus, to accept key input as integers, float, etc. we
must convert.
 To convert to int, use the Integer.parseInt(String)
function.
Ex. String s1 = “32”;
int j = Integer.parseInt(s1); // j now holds 32
 Converting to double is analogous:
Ex. String s1 = “32”;
double j = Double.parseDouble(s1); // j now holds 32
Parsing Strings
 Note that the conversion methods are just regular
procedural functions. That is, no object was
created in order to call the method. The entity
before the ‘.’ is _not_ an object. More on this
later.
 What if the String is unparseable? (e.g. “andrew”
rather than “32”).
 Study the Integer and Double classes and look for
a description of the errors that are thrown by the
various methods. Does anything stand out? See
example Looper.java. More on this later.
Reading Keyboard input at
runtime
 How do we prompt for keyboard input at
runtime?
 Use java.io.Scanner class with standard
input stream.
 Scanner in = new Scanner(System.in);
while (in.hasNext()){
String arg = in.next();
}
Thank you

More Related Content

What's hot (20)

PPTX
Python
Aashish Jain
 
PPT
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 
PPTX
Functional Programming In Jdk8
Bansilal Haudakari
 
PPTX
Unit1 introduction to Java
DevaKumari Vijay
 
PDF
Java Programming
Anjan Mahanta
 
PPT
Java platform
BG Java EE Course
 
PPTX
Java features
Prashant Gajendra
 
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
PPSX
Core java lessons
vivek shah
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PDF
Java8 features
Minal Maniar
 
PPTX
Java training in delhi
APSMIND TECHNOLOGY PVT LTD.
 
PPTX
Core java1
Ravi varma
 
DOCX
Java interview questions and answers for cognizant By Data Council Pune
Pankaj kshirsagar
 
PDF
Core Java Tutorial
eMexo Technologies
 
PPT
Java tutorial PPT
Intelligo Technologies
 
PPTX
Lecture - 1 introduction to java
manish kumar
 
PPTX
Polymorphism presentation in java
Ahsan Raja
 
PDF
Java notes | All Basics |
ShubhamAthawane
 
PDF
Java Interview Questions by NageswaraRao
JavabynataraJ
 
Python
Aashish Jain
 
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 
Functional Programming In Jdk8
Bansilal Haudakari
 
Unit1 introduction to Java
DevaKumari Vijay
 
Java Programming
Anjan Mahanta
 
Java platform
BG Java EE Course
 
Java features
Prashant Gajendra
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Core java lessons
vivek shah
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Java8 features
Minal Maniar
 
Java training in delhi
APSMIND TECHNOLOGY PVT LTD.
 
Core java1
Ravi varma
 
Java interview questions and answers for cognizant By Data Council Pune
Pankaj kshirsagar
 
Core Java Tutorial
eMexo Technologies
 
Java tutorial PPT
Intelligo Technologies
 
Lecture - 1 introduction to java
manish kumar
 
Polymorphism presentation in java
Ahsan Raja
 
Java notes | All Basics |
ShubhamAthawane
 
Java Interview Questions by NageswaraRao
JavabynataraJ
 

Similar to Basics java programing (20)

PPT
java01.ppt
Godwin585235
 
PPTX
Java programmingjsjdjdjdjdjdjdjdjdiidiei
rajputtejaswa12
 
PPT
JAVA_BASICS.ppt
VGaneshKarthikeyan
 
PPT
java_ notes_for__________basic_level.ppt
amisharawat149
 
PPT
java ppt for basic intro about java and its
kssandhu875
 
PPT
Java - A parent language and powerdul for mobile apps.
dhimananshu130803
 
PPT
Java intro
Sonam Sharma
 
PPT
Java01
Remon Hanna
 
PPT
Java01
Dhaval Patel
 
PPT
Present the syntax of Java Introduce the Java
ssuserfd620b
 
PDF
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
PPT
JAVA_BASICS_Data_abstraction_encapsulation.ppt
VGaneshKarthikeyan
 
PPT
java01.pptbvuyvyuvvvvvvvvvvvvvvvvvvvvyft
nagendrareddy104104
 
PPT
Java Simple Introduction in single course
binzbinz3
 
PPT
java01.ppt
SouravGhosh305827
 
PPTX
mukul Dubey.pptx
CodeHome
 
PPT
java01.ppt
ROGNationYT
 
PPT
java01.ppt
ShivamChaturvedi67
 
PPT
java01.ppt
FakeBuddy2
 
java01.ppt
Godwin585235
 
Java programmingjsjdjdjdjdjdjdjdjdiidiei
rajputtejaswa12
 
JAVA_BASICS.ppt
VGaneshKarthikeyan
 
java_ notes_for__________basic_level.ppt
amisharawat149
 
java ppt for basic intro about java and its
kssandhu875
 
Java - A parent language and powerdul for mobile apps.
dhimananshu130803
 
Java intro
Sonam Sharma
 
Java01
Remon Hanna
 
Java01
Dhaval Patel
 
Present the syntax of Java Introduce the Java
ssuserfd620b
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
JAVA_BASICS_Data_abstraction_encapsulation.ppt
VGaneshKarthikeyan
 
java01.pptbvuyvyuvvvvvvvvvvvvvvvvvvvvyft
nagendrareddy104104
 
Java Simple Introduction in single course
binzbinz3
 
java01.ppt
SouravGhosh305827
 
mukul Dubey.pptx
CodeHome
 
java01.ppt
ROGNationYT
 
java01.ppt
ShivamChaturvedi67
 
java01.ppt
FakeBuddy2
 
Ad

More from Darshan Gohel (8)

PPTX
Factors affecting Shelflife of food
Darshan Gohel
 
PPTX
Drying and dehydration of fruit crops
Darshan Gohel
 
PPTX
Soil mechanics
Darshan Gohel
 
PPTX
Preservative releasers as food packaging
Darshan Gohel
 
PPTX
Android application development
Darshan Gohel
 
PPTX
Introduction to computer science
Darshan Gohel
 
PPTX
ENGINEERING GRAPHICS-ED-FOR-BE/B.TECH-STUDENTS
Darshan Gohel
 
PPTX
TRANSMISSION SYSTEM
Darshan Gohel
 
Factors affecting Shelflife of food
Darshan Gohel
 
Drying and dehydration of fruit crops
Darshan Gohel
 
Soil mechanics
Darshan Gohel
 
Preservative releasers as food packaging
Darshan Gohel
 
Android application development
Darshan Gohel
 
Introduction to computer science
Darshan Gohel
 
ENGINEERING GRAPHICS-ED-FOR-BE/B.TECH-STUDENTS
Darshan Gohel
 
TRANSMISSION SYSTEM
Darshan Gohel
 
Ad

Recently uploaded (20)

PPTX
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
darshai cross section and river section analysis
muk7971
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
darshai cross section and river section analysis
muk7971
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Distribution reservoir and service storage pptx
dhanashree78
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 

Basics java programing

  • 2. Who is this class for?  There is no assumption that you know any Java or other OO programming language. If you do, you may find the first few weeks boring.  You must have programming experience. We will not spend very much time on syntax, procedural constructs, operators, etc. It is assumed that you can pick this up.
  • 3. History of Java  First version released in 1995  Eight major versions released since then – JDK 1.0 (1996) JDBC, Distributed Objects – JDK 1.1 (1997) New Event Model – J2SE 1.2 (1998) Swing – J2SE 1.3 (2000) Cleanup – J2SE 1.4 (2002) – J2SE 5.0 (2004) Generics – J2SE 6.0 (2006) – J2SE 7.0 (2011)
  • 4. Five Design Principles of Java  simple, object-oriented and familiar  robust and secure  architecture-neutral and portable  execute with "high performance  interpreted, threaded, and dynamic
  • 5. What is/isn’t Java?  Read chapter 1 of Core Java – Discussion is very balanced  Basically, compared to C Java is a relatively high- level language with many built-in features for portably doing useful things such as: – Multithreading – Writing distributed programs – Writing GUI clients – Error handling – Extending Web servers – Embedding programs in Web browsers – Connecting to commercial databases
  • 7. Java vs. C  Equally importantly, Java has many core language features that make it much more natural to express abstraction, develop software frameworks, etc.  Also, many core language features which ease debugging and promote reuse.
  • 8. Compiling/running first java program  Create source code file (call it for example MyFirstProgram.java).  To compile: prompt >> javac MyFirstProgram.java  This produces byte code file named MyFirstProgram.class  To run: prompt >> java MyFirstProgram
  • 9. Observations  .class file is not machine code. It is intermediate form called Java Byte code. Can run on any platform as long as platform has a Java Virtual Machine (JVM).  The second step on previous slide invokes the JVM to interpret the byte code on the given platform.  In theory, byte code can be moved to another platform and be run there without recompiling – this is the magic of applets.  Leave off the .class part when invoking the JVM.
  • 10. Observations  This is an old-fashioned command-line program. Java also supports GUI applications and web-browser hosted programs called applets.  After the first couple of weeks we will use graphical rather than scripting front-ends.
  • 11. Writing first program  To keep things simple our first few programs will all be written as just a single main program.  In java, the file in which your program resides must contain a .java extension (e.g. MyFirstProgram.java).
  • 12. Writing first program  Then, the program must be wrapped in a class definition which is the same as the file basename (MyFirstProgram). Careful, Java is case-sensitive. class MyFirstProgram { … }  Finally, main is defined similar to C, but with a few more modifiers: public static void main(String[] args){ … } These are all required. No shortcuts.
  • 13. Writing first program  Just as in C, main(..) is the principle entry point into the program. When you say java Program Java looks in Program for a procedure named main. This is where the program starts.  To print to stdout in java use: System.out.println(“ …”);  MyFirstProgram.
  • 14. Basic Programming Constructs What you should learn on your own
  • 15. Breakdown of a java program  Strategy to start is write evertything in a single main program and very quickly review the basics of java syntax (very little time here).  Then we break into procedures.  Then class/packages.
  • 16. Single-threaded program  For a single thread of execution, each line of code is executed sequentially (as in C).  Each statement is terminated with a semicolon.  Unlike C, declarations can occur anywhere within a program.  Basic operators, control statements almost exactly like C. A few minor differences.  Best to just look at some examples.
  • 17. Java Data Types  Sizes fully specified by Java standard.  Java is a very strongly typed language  Integer types – int (4 bytes signed) – short (2 bytes signed) – long (8 bytes signed) use suffix L (eg 1000000000L) – byte (1 byte signed)  Floating-point types – float (4 bytes) use suffix F (eg 1.28F) – double( 8 bytes)
  • 18. Additional Data Types  char – Two-byte unicode – Assignment with ‘ ‘ • e.g. char c = ‘h’;  boolean – true or false e.g. boolean x = true; if (x){…};
  • 19. Operators/Control Flow  Almost exactly like regular ANSI C.  +, *, -, /, %, ++, --, +=, etc.  ==, !=, >, < , etc.  if statements, for loops, while loops, do loops, switch statements, etc.  continue, break, return, System.exit(0).  Read pp 54– in Core Java.  No need to spend class time going over these.
  • 20. Scoping  Braces are used as in C to denote begin/end of blocks  Be careful of the following: int j = 0; if ( j <1 ){ int k = 2; … } k = 3; //Error! k inaccessible here  Declarations do not propogate upwards.
  • 21. Adding datatypes -- classes  Java has handful of built-in datatypes just discussed (int, float, etc.)  Just like in C, user typically creates own homemade datatypes to work with particular application (ie structs and enums).  In Java these are called classes.  Many class definitions come as a standard part of the Java distribution. Most common Example is String class.
  • 22. Strings  Java provides a class definition for a type called String  Since the String class is part of the java.lang package, no special imports are required to use it (like a header file in C).  Just like regular datatypes (and like C), variables of type String are declared as: String s1; String s2, s3; //etc.  Note that String is uppercase. This is the Java convention for classnames.
  • 23. Strings  Initializing a String is painless s1 = “This is some java String”;  Note that double quotes are required.  Memory is allocated dynamically.  Think of above method as shortcut for more standard way (assuming s1 has been declared): s1 = new String(“This is some java String”);  new operator required to create memory for new String object.
  • 24. String methods  Given a String object we can then access any public String method or instance variable (field).  Best to think of analogy with C. Given a variable of some struct type, we can access any of the struct’s members. If one of these members is a pointer to a function, we can essentially call a function using the struct. (x.doit(x,…))  In Java, this idea is taken quite a bit further, but the above analogy is a good start.
  • 25. String Examples  Best to see by way of example: String s = new String(“Hello”); Char c = s.charAt(3); System.out.println(c);  Method charAt called on String object s taking single integer parameter.  How might this look in a procedural language with structures? (homework)
  • 26. String class documentation  Incredibly important! – Each standard java class definition is fully documented online – You must become facile at reading/interpreting these documents. This is how everything is done in Java. – A little hard at first but then very simple. Makes learning new functionality much easier (if it’s well written). – Make a link to http://java.sun.com/j2se/1.3/docs/api/index.html  Much of homework will come from interpreting this page.
  • 27. 1d Arrays  Arrays in Java are dynamic; they are allocated with the new operator.  Creating a (1d) array is a two-step process: int[] x; //declare x to be an array of ints //x has the value of null right now x = new int[100]; //allocate 100 ints worth  At this point the values of x are all zeroes.  Assignment is then just like C.
  • 28. 1d Arrays  Note that in Java, unlike C, the compiler will not let you overrun the bounds of an array. Give it a try.  Note that you must use the new operator to size the array. It cannot be done statically as in C. Until this is done, the array reference has a value of null.  The array comes with a field called length which stores the number of elements in the array. Try printing x.length in the previous example. This is a simple but nice convenience.
  • 29. Misc tricks to get work done
  • 30. Parsings Strings  Recall that the args array is an array of Strings. Thus, to accept key input as integers, float, etc. we must convert.  To convert to int, use the Integer.parseInt(String) function. Ex. String s1 = “32”; int j = Integer.parseInt(s1); // j now holds 32  Converting to double is analogous: Ex. String s1 = “32”; double j = Double.parseDouble(s1); // j now holds 32
  • 31. Parsing Strings  Note that the conversion methods are just regular procedural functions. That is, no object was created in order to call the method. The entity before the ‘.’ is _not_ an object. More on this later.  What if the String is unparseable? (e.g. “andrew” rather than “32”).  Study the Integer and Double classes and look for a description of the errors that are thrown by the various methods. Does anything stand out? See example Looper.java. More on this later.
  • 32. Reading Keyboard input at runtime  How do we prompt for keyboard input at runtime?  Use java.io.Scanner class with standard input stream.  Scanner in = new Scanner(System.in); while (in.hasNext()){ String arg = in.next(); }