Java for android development

what level of java is required for android development and learn java for android development pdf free download
GraceRogers Profile Pic
Published Date:12-07-2017
Your Website URL(Optional)
Tutorial: Programming in Java for Android Development Adam C. Champion and Dong Xuan CSE 4471: Information Security Autumn 2013 Based on material from C. Horstmann 1, J. Bloch 2, C. Collins et al. 4, M.L. Sichitiu (NCSU), V. Janjic (Imperial College London), CSE 2221 (OSU), and other sources Getting Started (1) •  Need to install Java Development Kit (JDK) to write Java (and Android) programs –  Do not install Java Runtime Environment (JRE); JDK and JRE are different •  Can download the JDK for your OS at •  Alternatively, for OS X, Linux: –  OS X: •  Open /Applications/Utilities/   •  Type javac at command line •  Install Java when prompt appears –  Linux: •  Type sudo  apt–get  install  default–jdk at command line (Debian, Ubuntu) •  Other distributions: consult distribution’s documentation Getting Started (2) •  After installing JDK, download Android SDK from •  Simplest: download and install Android Developer Tools (ADT) bundle (including Android SDK) for your OS •  Alternatives: –  Download/install Android Studio (beta) from this site (based on IntelliJ IDEA) –  Install Android SDK tools by themselves, then install ADT for Eclipse separately (from this site) •  We’ll use ADT with SDK (easiest) Install Getting Started (3) •  Unzip ADT package to directory adt–bundle–os, then run adt–bundle–os/eclipse/Eclipse app. You should see this: Getting Started (4) •  Go to Eclipse preferences (Window→Preferences or ADT→Preferences), select Android tab •  Make sure the Android SDK path is correct (adt– bundle–os/sdk) •  Strongly recommend testing with real Android device –  Android emulator: very slow –  Install USB drivers for your Android device Getting Started (5) •  Bring up the Android SDK Manager –  Recommended: Install Android 2.2, 2.3.3 APIs and 4.x API –  Do not worry about Intel x86 Atom, MIPS system images •  In Eclipse, click Window→Show View→Other… and select views Android→Devices, Android→LogCat •  Now you’re ready for Android development Outline •  Getting Started •  Java: The Basics •  Java: Object–Oriented Programming •  Android Programming Java Programming Language •  Java: general–purpose language designed so developers write code once, it runs anywhere •  The key: Java Virtual Machine (JVM) –  Program code compiled to JVM bytecode –  JVM bytecode interpreted on JVM •  We’ll focus on Java 5 (Android uses this). See chapters 1–7 in 1. Our First Java Program (1) public  class  HelloWorld      public  static  void  main(String  args)          System.out.println(“Hello  world”);         •  Don’t forget to match curly braces , or semicolon at the end •  In Eclipse: File→New→Java Project, name project “HelloWorld” (no quotes), click Finish. Open HelloWorld in Package Explorer. Right–click src directory, select New→Class, and create a class HelloWorld with a main() method. To run the program, right–click HelloWorld project, click Run As Our First Java Program (2) Right–click, New→Class Explaining the Program •  Every .java source file contains one class –  We create a class HelloWorld that greets user –  The class HelloWorld must have the same name as the source file   –  Our class has public scope, so other classes can “see” it –  We’ll talk more about classes and objects later •  Every Java program has a method main() that executes the program –  Method “signature” must be exactly public  static  void  main(String  args)    ...     –  This means: (1) main() is “visible” to other methods; (2) there is “only one” main() method in the class; and (3) main() has one argument (args, an array of String variables) –  Java “thinks” main(), Main(), miAN() are different methods •  Every Java method has curly braces , surrounding its code •  Every statement in Java ends with a semicolon, e.g., System.out.println(“Hello  world”); •  Program prints “Hello  world” to the console, then quits Basic Data Types (1) •  Java variables are instances of mathematical “types” –  Variables can store (almost) any value their type can have –  Example: the value of a boolean variable can be either true or false because any (mathematical) boolean value is true or false –  Caveats for integer, floating–point variables: their values are subsets of values of mathematical integers, real numbers. Cannot assign 500 mathematical 2 to integer variable (limited range) or mathematical √2 to a floating–point variable (limited precision; irrational number). –  Variable names must start with lowercase letter, contain only letters, numbers, _   •  Variable declaration: boolean  b  =  true;   •  Later in the program, we might assign false to b: b  =  false;   •  Java strongly suggests that variables be initialized at the time of declaration, e.g., boolean  b; gives a compiler warning (null pointer) •  Constants defined using final keyword, e.g., final  boolean  falseBool  =  FALSE;  Basic Data Types (2) •  Java’s primitive data types: 5 Primitive type Size Minimum Maximum Wrapper type boolean   1–bit N/A N/A Boolean   16 char   16–bit Unicode 0 Unicode 2 – 1 Character   byte     8–bit –128 +127 Byte   15 15 short   16–bit –2 +2 – 1 Short   31 31 int   32–bit –2 +2 – 1 Integer   63 63 long   64–bit –2 +2 – 1 Long   float   32–bit IEEE 754 IEEE 754 Float   double   64–bit IEEE 754 IEEE 754 Double   Note: All these types are signed, except char. Basic Data Types (3) •  Sometimes variables need to be cast to another type, e.g., if finding average of integers: int  intOne  =  1,  intTwo  =  2,  intThree  =  3,  numInts  =  2;   double  doubOne  =  (double)intOne,  doubTwo  =  (double)myIntTwo,  doubThree  =   (double)intThree;   double  avg  =  (doubOne  +  doubTwo  +  doubThree)/(double)numInts;   •  Math library has math operations like sqrt(), pow(), etc. •  String: immutable type for sequence of characters –  Every Java variable can be converted to String via toString()   –  The + operation concatenates Strings with other variables –  Let str be a String. We can find str’s length (str.length()), substrings of str (str.substring()), and so on 6 Basic Data Types (4) •  A literal is a “fixed” value of a variable type –  TRUE, FALSE are boolean literals –  ‘A’, ‘\t’, ‘\”’, and ‘\u03c0’ are char literals (escaped tab, quote characters, Unicode value for \pi) –  –1, 0, 035, 0x1a are int literals (last two are octal and hexadecimal) –  0.5, 1.0, 1E6, 6.023E23 are double literals –  “At  OSU”, “Hello  world” are String literals •  Comments: –  Single-line: //  some  comment  to  end  of  line   –  Multi-line: /  comments  span  multiple  lines  /  Common Operators in Java String   boolean   char   int   double     ++  -­‐-­‐   +     +    -­‐   +    -­‐   &&      /    %      /                     =  =   =  =     ==  =   ==  =     Notes: •  Compare String objects using the equals() method, not == or =   •  && and use short-circuit evaluation. To see this, say boolean  canPigsFly  =  FALSE and we evaluate (canPigsFly  &&  some  Boolean  expression). Since canPigsFly is FALSE, the second part of the expression won’t be evaluated. •  The second operand of % (integer modulus) must be positive. •  Don’t compare doubles for equality. Instead, define a constant like so: final  double  EPSILON  =  1E-­‐6;  //  or  some  other  threshold     …          //  check  if  Math.abs(double1  –  double2)    EPSILON  Control Structures: Decision (1) •  Programs don’t always follow “straight line” execution; they “branch” based on certain conditions •  Java decision idioms: if-then-else, switch •  if-then-else idiom: if  (some  Boolean  expression)            //  take  some  action     else  if  (some  other  Boolean  expression)            //  take  some  other  action     else            //  do  something  else    Control Structures: Decision (2) •  Example: final  double  OLD_DROID  =  2.0,  final  double  NEW_DROID  =  4.0;   double  myDroid  =  4.1;   if  (myDroid    OLD_DROID)            System.out.println(“Antique”);     else  if  (myDroid    NEW_DROID)            System.out.println(“Very  modern”);     else            System.out.println(“Your  device:  barely  supported.”);       •  Code prints “Very  modern” to the screen. •  What if myDroid  ==  1.1? myDroid  ==  2.3? Control Structures: Decision (3) •  Example two: final  double  JELLY_BEAN  =  4.1,  final  double  ICE_CREAM  =  4.0;   final  double  EPSILON  =  1E-­‐6;   double  myDroid  =  4.1;   if  (myDroid    ICE_CREAM)            if  (Math.abs(myDroid  –  ICE_CREAM)    EPSILON)                            System.out.println(“Ice  Cream  Sandwich”);                    else                            System.out.println(“Jelly  Bean”);               else            System.out.println(“Old  version”);       •  Code prints “Jelly  Bean” to screen. Note nested if-then-else, EPSILON usage.

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.