본문으로 건너뛰기

FSD +003

· 약 4분

Terminology

  • Software: A set of statements written in a programming language to perform tasks
  • Statement: A single instruction in a program that performs an action when executed.
  • Snippet: A block of statements.
  • Software Development: The process of creating a software program.
  • OOP: Program composed of interconnected objects at runtime.
  • Expression: An entity-code component of a statement that can be evaluated to produce a value.
  • Assign: The process of storing the result (a value) of one or more expressions.
  • Value: A data item (literal or computed) that is stored in a variable.
  • Compiler: A special program that translates a programming language's source code into machine code.
    • Compilers complete the conversion process all at once after changes are made to the code and before the code is executed
  • Interpreter: A computer program that directly executes code without requiring it to be previously compiled into machine language.
    • Interpreters complete the conversion process one step at a time while the code is being executed.

Software development

  • Software development process is an iterative approach.
  • java
    • javac Welcome.java: Compiles the Java source file Welcome.java into class binary file.
    • java Welcome: Executes the Java program Welcome.
  • python
    • python welcome.py: Executes the Python script welcome.py.

OOP

  • Object: An object is a thing, tangible and intangible. An object has fields that contain the data and methods to access and modify the data.
  • Class: A class is an abstract definition of objects. A class is a template of a blueprint that defines what data and methods are included in objects.
  • Method: A block of code grouped together to perform an operation. A method has a name, parameters, and a return type.
  • Field: A field is a data attribute of an object. A field value is exposed using object methods.
  • Organizing code into classes improves modularity, reusability, extendability, and scalability.

Java vs Python

Identifier typeJavaPython
ClassUse CamelCase for multi-word classesUse snake_case for multi-word classes
Functionuse verbs or verb phrasesuse lowercase_with_underscores
Procedureuse verbs or verb phrasesuse lowercase_with_underscores
VariablecamelCaselowercase_with_underscores
ConstantAll uppercase words separated by underscoresAll uppercase words separated by underscores
PackageLowercase words separated by dotsLowercase words separated by underscores
  • Java uses the toString() function to return objects' information.
  • Python can refer to attributes directly or use the __str()__ function to return objects' information

Data types

Data TypeSizeDefault valueDescription
byte1 byte08-bit signed integer
short2 bytes016-bit signed integer
int4 bytes032-bit signed integer
long8 bytes064-bit signed integer
float4 bytes0.0f32-bit floating point
double8 bytes0.0d64-bit floating point
boolean1 bitfalsetrue or false
char2 bytes'\u0000'16-bit Unicode character

Non-Primitive Data Types

  • Non-primitive: Arrays, Classes, Interfaces, and Strings.
  • Non-primitive data types are by default set to null in Java, None in Python.

Variables

  • Static: enables the variable to be used without creating an object of its defining class.
  • Final: makes the variable unchangeable.

Operators

Operator CategoryJavaPython
Unaryexpr++ expr--
++expr --expr +expr -expr+expr -expr
Arithmetic* / &* / &
+ -+ -
Relational< > <= >=< > <= >=
== !=== !=
Logical! &&not and
||or
Ternary(expr1) ? <expr2> : <expr3>(expr1) if <expr2> then <expr3>
Assignment= += -= *= /= %== += -= *= /= %= **=
Identity/Membershipis is not in not in
  • Java: boolean q = (5 % 2 != 2) ? true : false
  • Python: q = True if (5 % 2 != 2) else False

Standard Input

import java.util.Scanner;

public class Inputs {
static Scanner in = new Scanner(System.in);

public static void main(String[] args) {
System.out.print("X = ");
int x = in.nextInt();
System.out.println("x squared = " + Math.pow(x, 2));
}
}
import sys

x = int(input("x = "))

print("x squared = ", pow(x, 2))

String

String (java)

Immutable

  • String s1 = "Hello";: initialize using literal syntax
  • String s2 = new String("Hello");: initialize using a constructor
s1 == s1 // false
s1.equals(s2) // true

String Format (Python)

SymbolMeaningExample codeOutput
<Left alignf'[{42:<5}]'[42 ]
>Right alignf'[{42:>5}]'[ 42]
^Center alignf'[{42:^5}]'[ 42 ]
< with fill charLeft align with custom fillf'[{42:-<5}]'[42---]
> with fill charRight align with custom fillf'[{42:->5}]'[---42]
^ with fill charCenter align with custom fillf'[{42:->5}]'[-42--]

Array

Array (java)

int[] x = {2, 4, -1, 11, 3};

  • Declaration: int[] x
  • Instantiation: x = new int[5];
  • Initialization: x[0] = 2; x[1] = 4; x[2] = -1;