Download Java Programming: Understanding Types, Variables, and Data Structures and more Slides Object Oriented Programming in PDF only on Docsity! Overview and intro to types docsity.com Welcome Learning about: • OO, abstract data types, generics, Java Collections, … • Reasoning about complex problems, analyzing algorithms we create to solve them, and implementing algorithms with elegant, easy-to-understand, correct code • Testing; Reasoning about correctness • Data structures: linked lists, trees, graphs, etc. • Recursion • Algorithmic complexity • Parallelism —threads of execution docsity.com Eclipse IDE • IDE: Integrated Development Environment • Helps you write your code • Protects against many common mistakes • At runtime, helps with debugging • Follow Resources link to download and install “In my country of Kazakhstan everyone is use Eclipse and Java! Java 1.7 is best for hack American web site and steal credit card.” docsity.com DrJava IDE • IDE: Integrated Development Environment • DrJava is a much simpler IDE, few features • We use it only to demo Java features and programming concepts. Has an “interactions pane”, which allows trying things without requiring a complete Java program. • DON’T use it for course assignments –use Eclipse • Free at www.drjava.org docsity.com Academic Integrity… Trust but verify! • We use artificial intelligence tools to check each homework assignment • The software is very accurate! • It tests your code and also notices similarities between code written by different people • Sure, you can fool this software • … but it’s easier to just do the assignments • … and if you try to fool it and screw up, you might fail the assignment or even the whole course. docsity.com Type: Set of values together with operations on them. Matlab and Python are weakly typed: One variable can contain at different times a number, a string, an array, etc. One isn’t so concerned with types. Valid Python sequence: x= 100; x= ‘Hello World’; x= (1, 2, 3, 4, 5 ); Corresponding Java int x; x= 100; x= “Hello”; Java strongly typed: A variable must be declared before it is used and can contain only values of the type with which it is declared Declaration of x: x can contain only values of type int Illegal assignment: “Hello” is not an int docsity.com Weakly typed versus strongly typed Weakly typed: Shorter programs, generally. Programmer has more freedom, language is more liberal in applying operations to values. Strongly typed: Programmer has to be more disciplined. Declarations provide a place for comments about variables. More errors caught at compile-time (e.g. it’s a syntax error to assign a string to an int variable). Note: weak and strong typing not well defined; literature has several definitions docsity.com Most-used ‘primitive’ types int: values: –231 .. 231–1 operations: +, –, *, /, %, unary – b % c : remainder when b is divided by c. 67 % 60 = 7 double: values like : –22.51E6, 24.9 operations: +, –, *, /, %, unary – Write values in “scientific notation” char: values like : 'V' '$' '\n' operations: none Use single quotes for type char. '\n' is new-line char boolean: values: true false operations: ! (not), && (and), || (or) Can’t use integers as booleans! Inside back cover, A-6..7 docsity.com Casting among types any number type any number expression byte short int long float double narrow wider must be explicit cast, may truncate may be automatic cast (int) 3.2 casts double value 3.2 to an int Page A-9, inside back cover (int) is a unary prefix operator, just like – – – 3 evaluates to 3 – (int) 3.2 evaluates to –3 15 docsity.com Char is a number type! Page A-9, inside back cover 16 char is a number type: (int) 'V' (char) 86 Unicode repr. in decimal: 86 'V' Unicode: 16-bit char repr. Encodes chars in just about all languages. In java, use hexadecimal (base 16) char literals: '\u0041' is 'A' '\u0042' is 'B' '\u0056' is 'V' '\u0024' is ‘$' See www.unicode.org '\u0950' is 'ॐ’ —Om, the sound of the universe '\u5927' is '大' —大衛 is (I think) a transliteration '\u885b' is '衛' of David into Chinese (Da Wei) docsity.com Basic Variable Declaration Page A-6 17 Declaration: gives name of variable, type of value it can contain int x; Declaration of x, can contain an int value 20.1area double double area; Declaration of area, can contain a double value 5x int int[] a; Declaration of a, can contain a pointer to an int array. We explain arrays much later a int[] docsity.com