Programming Articles

Page 2249 of 2544

Draw a line in C++ graphics

Ayush Gupta
Ayush Gupta
Updated on 23-Mar-2020 7K+ Views

In this tutorial, we will be discussing a program to draw a line in C++ graphics.To implement different shapes and sizes, animations, graphics.h library is used in C++.Example#include int main(){    int gd = DETECT, gm;    initgraph(&gd, &gm, "");    line(150, 150, 450, 150);    line(150, 200, 450, 200);    line(150, 250, 450, 250);    getch();    closegraph();    return 0; }Output

Read More

Delete and free() in C++ Program

Ayush Gupta
Ayush Gupta
Updated on 23-Mar-2020 198 Views

In this tutorial, we will be discussing a program to understand delete() and free() functions in C++.Both of these functions are primarily used for the same purpose i.e freeing up unused memory. The delete() operator is for the ones allocated using new() and free() for the ones allocated using malloc().Example#include #include int main(){    int x;    int *ptr1 = &x;    int *ptr2 = (int *)malloc(sizeof(int));    int *ptr3 = new int;    int *ptr4 = NULL;    //incorrect usage of delete    delete ptr1;    delete ptr2;    //correct usage of delete    delete ptr3;    delete ptr4;    getchar();    return 0; }

Read More

How can we execute snippets by ID in JShell in Java 9?

raja
raja
Updated on 23-Mar-2020 228 Views

JShell is an interactive tool (REPL) introduced in Java 9. We can execute snippets like expressions, variables, methods, classes, and etc without a main () method in the JShell tool.We can execute any prior snippet by simply typing /id, which indicates the snippet’s ID. For instance, if we type "/1", then JShell can display the first snippet that we have entered, executes it, and shows the result. We can re-execute the last snippet that we have entered (whether it is valid or invalid) by using "/!".In the below code snippet, we have created a set of snippets, and execute those snippets by using ...

Read More

Data types ranges and their macros in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Mar-2020 336 Views

Given the task is to find the memory range of the different data types, that what range of value a data type can store the value from minimum value to maximum value. There is memory range of data type from in which value of data can be stored. It is difficult to remember the large range of value so C++ has macros for represent these numbers, from macros these large numbers can be directly assigned to variable without typing the whole Number range.Example‘char’(signed) is character data type and has a range of -128 to +128 and macro for minimum value, ...

Read More

How can we avoid compilation errors in JShell in Java 9?

raja
raja
Updated on 20-Mar-2020 233 Views

JShell is an interactive tool that enables us to execute java code and displays the output instantly. JShell is the REPL(Read-Evaluate-Print-Loop) tool that runs from the command-line prompt. If we need to avoid the compilation-errors in JShell, then we must declare those variables before using it. The error message in JShell can use the notation "^--^" to highlight an error.In the below code snippet, the declaration of an int variable "div" attempts to use variables: num1, and num2 that has not been declared, so JShell reports a compilation error, indicating that the compiler was unable to find those variables.C:\Users\User>jshell | Welcome to JShell ...

Read More

What are the components in a module-info file in Java 9?

raja
raja
Updated on 20-Mar-2020 1K+ Views

A module is an independent unit of application that represents a single functionality. A module contains three important componentsName: To uniquely identify itDependencies: Other modules in which it depends onExported packages: Packages which are open for external applicationIn order to declare a module, we need to add the "module-info.java" file to the root source code. The components of the "module-info.java" file includes "name", "requires", "exports", and "exports to".Below is the template of "module-info.java" filemodule {    requires ;    requires ;    ...    exports ;    exports ;    ...    exports to ; }Name: It is ...

Read More

How can we display all module names in Java 9?

raja
raja
Updated on 19-Mar-2020 398 Views

In Java 9, the module concept has introduced. It is a named, self-describing collection of code and data. The code can be organized as a set of packages containing types like java classes and interfaces, and data includes resources and other kinds of static information. A module contains a name, dependencies, and exported packages.Syntaxmodule com.tutorialspoint.mymodule {    // some statements }In the below example, we can able to display all module names by using the ModuleLayer class.Examplepublic class AllModulesNamesTest {    public static void main(String args[]) {       ModuleLayer.boot().modules().forEach((module) -> {          System.out.println(module.getName());       ...

Read More

How to print the pattern of stars in JShell in Java 9?

raja
raja
Updated on 19-Mar-2020 317 Views

JShell is a REPL tool introduced in Java 9 that allows us to execute Java code and getting results immediately. We can evaluate expressions or simple algorithms without creating a new project, compile or build it by using JShell. We can also execute expressions, use imports, define classes, methods, and variables. It is a part of Java 9 JDK but not JRE.We can start JShell session in command-prompt by simply typing jshell. We can use different commands: /exit to quit the JShell session, reset/reload JShell anytime by typing /reset, and /reload,  /import to list the imports, etc.In the below example, we can print ...

Read More

What are Compact Strings in Java 9?

raja
raja
Updated on 19-Mar-2020 411 Views

Since Java 9, the JVM optimizes strings by using a new feature called Compact Strings. Instead of having a char[] array, a string can be represented as a byte[] array. We can use either UTF-16 or Latin-1 to produce either one or two bytes per character. If JVM detects the string contains only ISO-8859-1/Latin-1 characters, then string uses one byte per character internally.The string can be represented with a compact string or not is detected when the string is created. This feature has enabled by default and switches off using the -XX:-CompactStrings. It doesn't revert to a char[] implementation and stores all strings as ...

Read More

How to display all stack frames of the current thread in Java 9?

raja
raja
Updated on 19-Mar-2020 473 Views

Stack Walking API can provide a flexible mechanism to traverse and extract information from call stacks that allow us to filter and access frames in a lazy manner. StackWalker class is an entry point to Stack Walking API. The stack trace is a representation of a call stack at a certain point of time in which each element represents a method invocation. It contains all invocations from the start of a thread until the point it’s generated.In the below example, we can print/display all stack frames of the current thread by using StackWalker API.Exampleimport java.lang.StackWalker.StackFrame; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; public ...

Read More
Showing 22481–22490 of 25,433 articles
Advertisements