C and Java some big differences

Similar documents
Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Getting started with Java

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Final CSE 131B Spring 2004

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Lec 3. Compilers, Debugging, Hello World, and Variables

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

PROGRAMMING FUNDAMENTALS

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Lecture Set 4: More About Methods and More About Operators

Programming refresher and intro to C programming

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Lecture Set 4: More About Methods and More About Operators

Computational Expression

printf( Please enter another number: ); scanf( %d, &num2);

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

CSE 333 Lecture 2 Memory

Following is the general form of a typical decision making structure found in most of the programming languages:

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Fundamentals of Programming Session 19

Motivation was to facilitate development of systems software, especially OS development.

Kickstart Intro to Java Part I

CS61C : Machine Structures

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CS61C : Machine Structures

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Motivation was to facilitate development of systems software, especially OS development.

C - Basics, Bitwise Operator. Zhaoguo Wang

CS251L REVIEW Derek Trumbo UNM

An overview of Java, Data types and variables

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Principles of Programming Languages. Lecture Outline

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Lecture 1: Overview of Java

Java Bytecode (binary file)

CS 211: Methods, Memory, Equality

Array. Array Declaration:

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Information Science. No. For each question, choose one correct answer and write its symbol (A E) in the box.

Topics Introduction to Microprocessors

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Introduction to C An overview of the programming language C, syntax, data types and input/output

CS 61C: Great Ideas in Computer Architecture Introduction to C

Lecture 2, September 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Lectures 5-6: Introduction to C

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Java Identifiers, Data Types & Variables

Programming Language Concepts: Lecture 2

Topic 6: A Quick Intro To C

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Exercise Session 2 Simon Gerber

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS 11 java track: lecture 1

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS61C : Machine Structures

2. The object-oriented paradigm

large units some names for large numbers CIS 2107 Chapter 2 Notes Part 1

CIS 2107 Chapter 2 Notes Part 1. Representing and Manipulating Information

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Simple Java Reference

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Kurt Schmidt. October 30, 2018

Review for Test 1 (Chapter 1-5)

Page 1. Agenda. Programming Languages. C Compilation Process

Introduction to Programming Using Java (98-388)

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Final CSE 131B Spring 2005

Section 2.2 Your First Program in Java: Printing a Line of Text

Basic Assignment and Arithmetic Operators

Fall 2017 CISC124 9/16/2017

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Compiling Techniques


Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Lectures 5-6: Introduction to C

CS3157: Advanced Programming. Outline

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Java. Representing Data. Representing data. Primitive data types

Loops. CSE 114, Computer Science 1 Stony Brook University

Announcements. 1. Forms to return today after class:

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Transcription:

and some big differences Some August 30, 016 object-oriented vs procedural non-interpreted vs interpreted memory management references vs. free, unrestricted pointers error handling A Very Simple Program A Very Simple Program 1 public class Welcome { public static void main(string args[]) { 3 System.out.println("Welcome to IS 107"); 4 5 1 3 4 printf("welcome to IS 107\n"); 5 6 1 public class Welcome { public static void main(string args[]) { 3 System.out.println("Welcome to IS 107"); 4 5 s main( ) starting point of the program returns int, not void return status 0 OK 1 3 4 printf("welcome to IS 107\n"); 5 6 int argc, char **argv same as s String args

A Very Simple Program ompiling and Running in Stuff.java 1 public class Welcome { public static void main(string args[]) { 3 System.out.println("Welcome to IS 107"); 4 5 #include 1 3 4 printf("welcome to IS 107\n"); 5 6 idea not unlike import. different mechanism preprocessor. text mangling public class Stuff { javac Stuff.class 1101 0101 0110 1000 java PU ompiling and Running in... but it s really not that bad printf.o formal gcc -o executable_file_name source_file_name welcome.c Source program (text) Preprocessor (cpp) welcome.i Modified source program (text) ompiler (gcc) welcome.s Assembler (as) Assembly program (text) welcome.o Linker (ld) Relocatable object programs (binary) welcome Executable object program (binary)

... but it s really not that bad... but it s really not that bad formal gcc -o executable_file_name source_file_name formal gcc -o executable_file_name source_file_name example gcc -o stuff stuff.c example gcc -o stuff stuff.c and to execute./stuff Data Types Primitives primitives all types objects integer types byte short int long floating-point types float double characters char boolean boolean

Primitives vs. Types Primitives Sizes integer types byte short int long floating-point types float double characters char boolean boolean integer types char short int long floating-point types float double characters char boolean any integer type type size (bytes) byte 1 short int 4 long 8 float 4 double 8 char boolean 1 bit Why are About Size? Why are About Size? For example, if s byte and long can both represent integers, why use one instead of the other? For example, if s byte and long can both represent integers, why use one instead of the other? large size > values you can represent small size < memory used More on this later

Types Sizes Printing a String type size (bytes) char 1 short? int? long? float? double? printf("welcome to IS 107\n"); Format Strings Format Strings int x=10; printf("x is %d\n", x); int x=10; printf("x is %d\n", x); placeholder

Format Strings Format Strings. Multiple Placeholders int x=10; printf("x is %d\n", x); int x=10, y=0, z=30; printf("x is %d, y=%d, z=%d\n, x, y, z); placeholder x is 10 Format Strings. Multiple Placeholders Format Strings. Different Formats int x=10, y=0, z=30; printf("x is %d, y=%d, z=%d\n, x, y, z); int x=10, y=0, z=30; printf("x is %x, y=%x, z=%x\n", x, y, z); x is 10, y is 0, z is 30

Format Strings. Different Formats What Was the Deal with char Again? int x=10, y=0, z=30; char x=65; printf("x is %x, y=%x, z=%x\n", x, y, z); x++; printf("x is %d\n", x); x is a, y=14, z=1e What Was the Deal with char Again? an Also Be Used to Represent haracters char x=65; x++; printf("x is %d\n", x); char x= A ; printf("x is %c\n", x); x is 66

an Also Be Used to Represent haracters if char x= A ; printf("x is %c\n", x); x is A 1 if (some condition) statement; 3 4 if (some other condition) { 5 statement_1; 6 statement_; 7... 8 statement_n; 9 1 if (some condition) statement; 3 4 if (some other condition) { 5 statement_1; 6 statement_; 7... 8 statement_n; 9 if-else for loop 1 if (some condition) statement_1; 3 else 4 statement_; 5 6 if (some condition) { 7 statement_1; 8 statement_; 9... 10 else { 11 statement_3; 1 statement_4; 13... 14 1 if (some condition) statement_1; 3 else 4 statement_; 5 6 if (some condition) { 7 statement_1; 8 statement_; 9... 10 else { 11 statement_3; 1 statement_4; 13... 14 Also the same as generic 1 for (initial condition; test; update) statement;

for loop for loop Also the same as generic 1 for (initial condition; test; update) statement; example 1 for (i=0; i<len; i++) A[i]+=; Also the same as generic 1 for (initial condition; test; update) statement; example 1 for (i=0; i<len; i++) A[i]+=; another example 1 for (i=0, j=len-1; i<j; i++, j--) swap(a, i, j); Loop ontrol Variables boolean public static void main(string args[]) { for (int i=0; i<max; i++) { for (int i=max; i>0; i--) { for (int i=0; i<thresh; i++) { int i; for (i=0; i<max; i++) { for (i=max; i>0; i--) { expression result result 10<0 true 1 10>0 false 0 for (i=0; i<thresh; i++) {

means you can have things like but also means... if (1) { /* always runs */ if (0) { /* never runs */ 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 int i=50; while (i) { i--; but also means... so then you get confused, angry and add 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 equal 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 8 9 printf("x=%d, y=%d\n", x, y);

so then you get confused, angry and add so then you get confused, angry and add 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 8 9 printf("x=%d, y=%d\n", x, y); equal x=0, y=0 1 int x=10, y=0; 3 if (x=y) { 4 printf("equal\n"); 5 else { 6 printf("not equal\n"); 7 8 9 printf("x=%d, y=%d\n", x, y); What s Happening? assignment then test no compiler error Things that are almost completely the same in and Arrays if, if-else for, while, do-while switch though not Strings. operators +, -, mostly comments: mostly /* supported everywhere */ // mostly supported OK int A[5]; int A[]={10,0,30,40,50;

Arrays Arrays OK int A[5]; int A[]={10,0,30,40,50; Not OK int A[]; int []A={10,0,30,40,50; OK int A[5]; int A[]={10,0,30,40,50; Not OK int A[]; int []A={10,0,30,40,50; Legal but will get you in trouble int A[10]; A[15]=555; A[-3]=5; Arrays What do we pass when we pass an array in? size no.length field pass length with array